I tried to get a clue if there is a performance leak in the posted code or not so i wrote a litte check Consoleapp for your Problem. (Excluding the Update of the Database off course).
It would give me a great pleasure if you would run this "analysis" with your code and post us the results.
using System;
using System.IO;
using System.Diagnostics;
namespace PerformanceCheck
{
class MainClass
{
public static void Main (string[] args) {
Console.WriteLine ("Press enter to write file");
Console.ReadLine ();
FileStream stream = null;
TextWriter writer = null;
try {
FileInfo info = new FileInfo ("/tmp/file.txt");
if(info.Exists)
info.Delete();
stream = info.OpenWrite ();
writer = new StreamWriter (stream);
for (int i = 0; i< 500; i++) {
writer.WriteLine ("S01\tsdf\tsdf\tsdf\tShipped\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\t" + i);
writer.WriteLine ("S01\tsdf\tsdf\tsdf\tUnshipped\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\t" + i);
writer.WriteLine ("S02\tsdf\tsdf\tsdf\tShipped\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\t" + i);
writer.WriteLine ("S02\tsdf\tsdf\tsdf\tUnshipped\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\tsdf\t" + i);
}
Console.WriteLine ("Successfully written file!");
} catch (Exception ex) {
Console.WriteLine ("Dude, Exception!");
Console.WriteLine (ex.Message);
Console.WriteLine ("Press enter to quit;");
Console.ReadLine ();
return;
} finally {
if (writer != null)
writer.Flush ();
if (stream != null)
stream.Close ();
}
Console.WriteLine ("Press enter to insert Data into Database");
Console.ReadLine ();
sold4weeks1 ();
Console.WriteLine ("Data updated, press enter to quit");
Console.ReadLine ();
}
static void sold4weeks1 () {
Stopwatch watch = new Stopwatch ();
string sold, asin;
watch.Start ();
string[] lines = System.IO.File.ReadAllLines ("/tmp/file.txt");
Console.WriteLine ("Reading lines took: " + watch.ElapsedMilliseconds + "ms");
watch.Reset ();
watch.Start ();
Stopwatch lineReadWatch = new Stopwatch ();
try {
foreach (string line in lines) {
lineReadWatch.Start ();
char[] tabs = { '\t' };
string[] words = line.Split (tabs);
asin = words [12];
sold = words [14];
if (words [0].Substring (0, 3) == "S01")
continue;
try {
if (words [4] == "Shipped" || words [4] == "Unshipped") {
int i = Convert.ToInt16 (sold);
Console.WriteLine ("Update: "+i);
}
} catch {
} finally {
lineReadWatch.Stop ();
lineReadWatch.Reset ();
Console.WriteLine ("Handling of line took: " + lineReadWatch.ElapsedMilliseconds + "ms");
}
}
} catch {
}
Console.WriteLine ("Whole routine took: " + watch.ElapsedMilliseconds + "ms");
watch.Stop ();
}
}
}
Developed and successfully tested on Mono/OSX
As far as i can see, yes there are some points where performance can be gained, but i would be intrested, if this is the real bottleneck. As the others before i believe the huge performance gains can be made in the database update code.
But I'm ready to be profen wrong and so I would be verry interested in your performance mesurements!
Output Snippet:
Handling of line took: 0ms
Update: 499
Handling of line took: 0ms
Whole routine took: 38ms
Data updated, press enter to quit