bgrosz

bgrosz

  • NA
  • 1
  • 0

Surprising performance difference

Jun 23 2004 6:08 PM
Hello, I recently stumbled across a huge performance difference involving writing out a large hash table to a file. The hashtable is holding a very simple object called Token, that holds a string value called token and a couple counters. ToString() is overriden for the object to return: tokenstring [space] counter1 [space] counter2 If I iterate through the values of the hashtable and write it out to a file, it takes around 1 minute and 40 seconds to complete. Seems a little slow. Here is the weird part. If I iterate through the values of the hashtable and add them to an arraylist and then iterate through the arraylist to write out the values, it takes between 10 and 12 seconds. The hash table is large, as the file that is written out is 39 Mb where each line is around 18 characters, where each line represents one hash table entry. Below is some of my test code to clarify the situation if my explanation above has anyone confused. WriteTokensSlow takes about 1 minutes and 40 seconds. WriteTokensFast takes between 10 and 12 seconds. private void WriteTokensSlow() { WriteCollection(tokensHT.Values); } private void WriteTokensFast() { ArrayList tokenList = new ArrayList(tokensHT.Values); WriteCollection(tokenList); } private void WriteCollection(ICollection collection) { using (StreamWriter sw = new StreamWriter(savePath,false)) { foreach (Token tokenItem in collection) sw.WriteLine(tokenItem.ToString()); } } Can anyone explain why this is happening? Thanks.