I'm seeing a 3x-10x performance slowdown using the SortedDictionary class in 64-bit CLR. I posted a longer thread earlier but just deleted it, since I now understand the problem in more detail and have a much shorter repro case (included below) so wanted to start fresh.
The code below runs at least 3x slower on an 8-wayx86 server than all the 32-bit PCs I tried including a beefy 4-way Xeon server and a low-end workstation. I can create variations on this code which run even slower, but figured simpler is better for now.
CLR experts-- is this expected? Could there be something else causing the problem besides 64 vs. 32 bit, e.g. 8-way vs. 4-way? Note that I haven't seensimilar variations with other CLR classes, only SortedDictionary. The problem doesn't occur with smaller-sized dictionaries, only larger ones. (My test uses 20K items)
using
System;
using
System.Collections.Generic;
class
Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting perf test of SortedDictionary");
Random rand = new Random();
DateTime start = DateTime.Now;
int nTotalDictionaries = 500, dictSize = 20000, nTotalItems = 0;
for (int j = 0; j < nTotalDictionaries; j++)
{
SortedDictionary<int, bool> dict = new SortedDictionary<int, bool>();
for (int i = 0, nLen = dictSize; i < nLen; i++, nTotalItems++)
{
// get a new index, and make sure it's not taken already
int newNumber;
for (newNumber=rand.Next(1,1000000); dict.ContainsKey(newNumber);newNumber=rand.Next(1,1000000))
;
dict.Add(newNumber, true); // add it
}
}
Console.WriteLine ("Running in {0}-bit mode", IntPtr.Size * 8);
TimeSpan elapsed = (DateTime.Now - start);
Console.WriteLine("Loaded {0} total items, into {1} SortedDictionary instances in {2} seconds",
nTotalItems, nTotalDictionaries, elapsed.TotalMilliseconds / 1000.0);
Console.WriteLine("press any key to continue");
Console.ReadKey();
}
}