Search code examples
c#.netmulticore

Using all cores in pc with c#


*Please note that I am just testing to understand this.

I am trying to use all the cores of my computer with the Parallel.For() method. This is working just fine, but when I am trying the same method with a normal for loop it is going much faster. The parallel method is taking 16 sec and the normal method is only taking 6 sec.

I hope you can tell me what I am doing wrong here.

Updated code

        DateTime parallelStart = new DateTime();
        DateTime parallelFinish = new DateTime();
        DateTime singeStart = new DateTime();
        DateTime singeFinish = new DateTime();
        parallelStart = DateTime.Now;
        int inputData = 0;

        Parallel.For(0, 1000000000, i =>
        {
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
        });

        parallelFinish = DateTime.Now;
        singeStart = DateTime.Now;

        for (int i = 0; i < 1000000000; i++)
        {
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
        }

        singeFinish = DateTime.Now;
        MessageBox.Show("Parallel execution time: " + (parallelFinish - parallelStart).Seconds + "\n" +
                        "Singe execution time: " + (singeFinish - singeStart).Seconds);

First code:

DateTime parallelStart = new DateTime();
DateTime parallelFinish = new DateTime();
DateTime singeStart = new DateTime();
DateTime singeFinish = new DateTime();
parallelStart = DateTime.Now;

Parallel.For(0, 2000000000, i =>
{
    var inputData = 0;
});

parallelFinish = DateTime.Now;
singeStart = DateTime.Now;

for (int i = 0; i < 2000000000; i++)
{
    var inputData = 0;
}

singeFinish = DateTime.Now;
MessageBox.Show("Parallel execution time: " + (parallelFinish - parallelStart).Seconds + "\n" + "Singe execution time: " + (singeFinish - singeStart).Seconds);

Solution

  • Setting the value of an existing value type on the stack is incredibly, profoundly trivial. The cost of creating and keeping track of the threads would be large compared to this. Adding any complexity to your operation would drastically change your results. Even creating and setting a string variable equal to the appending of two strings would massively increase the work done. Try something more complicated (almost anything will do!) and you'll see the value of the parallel library increase.

    Try this:

    Parallel.For(0, 2000000000, i =>
    {
        var string inputData = "ninjas " + "like " + "taco";
    });
    

    Believe it or not, we have added a lot more processing here. (There's multiple strings being created and tossed in the background) This will change your results. And then when you consider that we do vastly more complicated things in these loops, like:

    • data-access
    • disk IO
    • complicated math
    • filtering / ordering / projecting of lists, etc.

    , the results will be dramatic in favor of parallelism.