Testing multicore in C#

After taking a look at: GenbetaDev – Benchmark
I have benchmark the code by my own using multicore and different collections, My conclusions are:
(Spanish)
Despues de echar un vistazo a:GenbetaDev – Benchmark
He realizado el test por mi cuenta para probar el multicore con colecciones diferentes, mis conclusiones son:

1.- The Log operation is too simple in order to benchmark, it needs something more complex.
2.- The use of the Generic.Collections instead of the Concurrent is faster in these cases but doesn´t use correctly the Multicore.
3.- The best and optimized code I found is:

(Spanish)
1.- La operacion de Log es demasiado simple para el benchmark.
2.- El uso de Generic.Collections en vez de concurrents es más rapido en este test, pero no usa correctamente el multicore.
3.- El mejor codigo que he realizado es:

static void Main(string[] args)
        {
            DateTime d0, d1;
            Int32 capacity = 5000000, time =-1, times = 20, cores = 1;
            d0 = DateTime.Now;
            ConcurrentDictionary<float, float> bag = new ConcurrentDictionary<float, float>(cores,capacity*times);
            while (++time < times)
            {
                Parallel.For(0, capacity - 1, new ParallelOptions() { MaxDegreeOfParallelism = cores }, i =>
                    {
                        bag.TryAdd(i,(float)Math.Log(i));
                    });
            }
            d1 = DateTime.Now;
            TimeSpan t = d1 - d0;
            Console.Write(t.TotalMilliseconds.ToString());
            Console.Read();
        }

The benchmark result , Los resultados de las pruebas:
(spi => seconds per loop iteration)

Dictionary Iteraciones = 1×5000000

1 Core  = 405 ms => .4spi
2 Cores = 608 ms => .6spi
3 Cores = 748 ms => .7spi
4 Cores = 826 ms => .8spi

Dictionary Iteraciones = 100×5000000

1 Core  = 41435,5004 ms => .4spi
4 Cores = 80449,3414 ms => .8spi

List Iteraciones 1×5000000 casting float

1 Core   = 308,8021 ms => .3spi
4 Cores  = 561,6009 ms => .8spi

List Iteraciones 100×5000000 casting float

1 Core   = 27300,0479 ms => .3spi
4 Cores  = 61807,3085 ms => .6spi

List Iteraciones 1×5000000 sin/without casting

1 Core   = 312,005 ms => .3spi
4 Cores  = 561,601 ms => .5spi

List Iteraciones 100×5000000 sin/without casting

1 Core   = 27284,448 ms => .3spi
4 Cores  = 62433,314 ms => .6spi

Sin almacenar (Without storage) Iteraciones 100×5000000 sin/without casting

1 Core   = 24273,6423 ms => .2spi
2 Cores  = 12620,4222 ms => .1spi
3 Cores  = 9032,4158 ms => .090 spi
4 Cores  = 6973,2122 ms => .069spi

So the issue is in the storage management, let’s try to improve
Es decir el problema está en el almacenamiento en memoria, vamos a mejorarlo

ConcurrentBag Iteraciones 1×5000000

1 Core   = 1544,4027 ms => 1.5spi
4 Cores  = 1201,2021 ms => 1.2spi

ConcurrentBag Iteraciones 10×5000000

1 Core   = 16999,228 ms => 1.7spi
4 Cores  = 13135,223 ms => 1.3spi

ConcurrentDictionary Iteraciones 10×5000000

1 Core   = 32416,856 ms => 3.2spi
4 Cores  = 32120,456 ms => 3.2spi

ConcurrentQueue Iteraciones 10×5000000

1 Core  = 7893,6139 ms  => .8spi
4 Cores = 10365,229 ms =>  1.0spi

Let’s optimize as full as I can the ConcurrentDictionary
Vamos a optimizar todo lo que se pueda el ConcurrentDictionary

ConcurrentDictionary 1×5000000

1 Core  = 9467,6216 ms => 2.0spi
4 Cores = 12464,42 ms => 2.6spi

ConcurrentDictionary 10×5000000

1 Core  = 9467,6216 ms => .94spi
4 Cores = 12464,42 ms => 1.24spi

ConcurrentDictionary 20×5000000

1 Core  = 17303,2323 ms => .86spi
4 Cores = 22089,6388 ms => 1.1spi

So I recommend the use of Concurrent and Parallel when you have heavy operations inside the Parallel for, in other case use the simple way.
Por tanto recomiento el uso de concurrent y parallel cuando tengas operaciones pesadas dentro del bucle, en otro caso usar la forma normal.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s