Wednesday, February 24, 2010

C# - the fastest way to iterate over dictionary

Having a big dictionary, say 1M of <double,double> samples – what is the fastest way to iterate over it?
Comparing 3 options (iterate over KeyValuePair in the dictionary, iterate over Values collection and iterate over Keys collection) – below is code and timing comparison results.
// Of course if you iterating over Keys collection and accessing the value by key – it would be the slowest case.

// Create sample dictionary
var random = new Random();
var randomDictionaryOfDoubles =
    Enumerable.Repeat(0, 1000000).Select(
        i => Convert.ToDouble(random.Next()))
        .ToDictionary(i => i);
double total = 0.0;
var stopWatch = new Stopwatch();
//Console.WriteLine("Starting KVP");
foreach (KeyValuePair<double, double> kvp in randomDictionaryOfDoubles)
    total += (kvp.Value);
Console.WriteLine("KVP took:    {0} Total = {1}", stopWatch.Elapsed, total);
//Console.WriteLine("Starting Values");
total = 0.0;
// To get the values alone, use the Values property.
Dictionary<double, double>.ValueCollection valueColl = randomDictionaryOfDoubles.Values;
foreach (double d in valueColl)
    total += d;
Console.WriteLine("Values took: {0} Total = {1}", stopWatch.Elapsed, total);
//Console.WriteLine("Starting keys");
total = 0.0;
// To get the keys alone, use the Keys property.
Dictionary<double, double>.KeyCollection keyColl = randomDictionaryOfDoubles.Keys;
foreach (double d in keyColl)
    total += d;
Console.WriteLine("Keys took:   {0} Total = {1}", stopWatch.Elapsed, total);

Here are results:

Method Time spent in iteration
Over key-value pairs 00:00:00.1854684
Over Values collection 00:00:00.1221380
Over Keys collection 00:00:00.0955291

Related article:

Technorati Теги: ,,

Tuesday, February 23, 2010

Copy code as HTML from VS 2008 into Live Writer

The best option for me is this:

Technorati Теги: ,

Probability to observe the same number calling Random.Rand()

It’s not an efficient query. Just a game…
for (int j = 1; j < 8; j++)
    int numElements = (int)Math.Pow(10, j);
    var numersList = Enumerable.Repeat(0, numElements).Select(
        i => Convert.ToDouble(random.Next()))
        .Distinct().OrderBy(i => i);
    double probabilityGetSameNumber = 1 - (double) numersList.Count()/numElements;
        "Rand() was called {0} times. Same number was observed in {1} % cases",
        numElements, probabilityGetSameNumber);


Num of Rand() calls Same number observed in % of cases Cases count
10 0 0
100 0 0
1000 0 0
10000 0 0
100000 0.00002999999999997 3
1000000 0.00024100000000004 241
10000000 0.00234760000000001 23476


Technorati Теги: ,,,

Random clicks

Wednesday, February 17, 2010

Impossible to log SQL 2008 performance counters under Windows 2008 64 bit?

Here is some strange problem I’ve experienced (right now it seems to me as a bug in Windows 2008 64 bit):

I have an environment with Windows 2008 64 bit and SQL Enterprise 64 bit.

To begin with - it's possible to see SQL-related performance counters values only in perfmon opened from SysWow64 (i.e. 32 bit) - they are not present in 64-bit perfmon.

Now, if I open perfmon (32 bit) and add any SQL counters (e.g. SQL Latches) to the "Performance Monitor" section I can see that those values are updated "live" in perfmon graphs.

Yet, if I create a new Data Collector Set for the same values under Data Collector Sets -> User Defined - the values for SQL counters are not being stored in the created log file. If I add some other counters (e.g. Processor) they ARE saved.

Seems that this is some sort of bug/problem related to 64 bit…

There are some clues on this problem in two following articles:

but both do not help (for the first one – there is no such service anymore in Windows 2008…

Many, many pretties... piled high beyond the sky

Yes, it could be a lot more small rectangles in it, I know ;o)
(But you can't look too far - when you look too far - it looks back).
Strong Server
Technorati Tags: ,