In your opinion, how many CLR object can be created in one second?

The following console application and two copies conventional parallel processing version of a simple test for this measure provides:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace ObjectInitSpeedTest
{
    class Program
    {
        static void Main()
        {
            normalSpeedTest();
            parallelSpeedTest();
            Console.Title = "CLR Speed test";
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("http://www.ehsanenaloo.com");
            Console.WriteLine("Press any key ...");
            Console.ReadKey();
        }

        private static void parallelSpeedTest()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("ParallelSpeedTest");

            long totalObjectsCreated = 0;
            long totalElapsedTime = 0;

            var tasks = new List<Task>();
            var processorCount = Environment.ProcessorCount;

            Console.WriteLine("Running on {0} cores", processorCount);

            for (var t = 0; t < processorCount; t++)
            {
                tasks.Add(Task.Factory.StartNew(
                () =>
                {
                    const int reps = 1000000000;
                    var sp = Stopwatch.StartNew();
                    for (var j = 0; j < reps; ++j)
                    {
                        new object();
                    }
                    sp.Stop();

                    Interlocked.Add(ref totalObjectsCreated, reps);
                    Interlocked.Add(ref totalElapsedTime, sp.ElapsedMilliseconds);
                }
                ));
            }

            Task.WaitAll(tasks.ToArray());

            Console.WriteLine("Created {0:N} objects in 1 sec\n", (totalObjectsCreated / (totalElapsedTime / processorCount)) * 1000);
        }

        private static void normalSpeedTest()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("NormalSpeedTest");

            const int reps = 1000000000;
            var sp = Stopwatch.StartNew();
            sp.Start();
            for (var j = 0; j < reps; ++j)
            {
                new object();
            }
            sp.Stop();

            Console.WriteLine("Created {0:N} objects in 1 sec\n", (reps / sp.ElapsedMilliseconds) * 1000);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *