Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why is the parallel loop not any faster than the sequential loop?
C#
    Stopwatch sw = new Stopwatch();
    Stopwatch sw2 = new Stopwatch();
    sw.Start();
    // Using a named method.
    for (int i = 0; i < 10000; i++)
    {
        Console.WriteLine(i);
    }

    sw.Stop();

    sw2.Start();
    // Using a named method.
    Parallel.For(0, 10000, Method2);
    sw2.Stop();
    Console.WriteLine("Seq " + sw.ElapsedMilliseconds);
    Console.WriteLine("Parallel " + sw2.ElapsedMilliseconds);

static void Method2(int i)
{
    Console.WriteLine(i);// Do work.
}
Posted
Updated 4-Dec-12 17:58pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Dec-12 0:25am    
Even though this question slightly different from your previous one, this is re-post, which usually does not help.
--SA

Please see Parallel For loops in C#[^] and please don't re-post.

Your re-post of your previous question clearly shows that you largely ignored my answer where I already explained what you asked in this question. If you did not understand my performance considerations, you would explain what your concerns are referring to that answer.

—SA
 
Share this answer
 
v2
Because the console can only write one line at a time, no matter how many threads you have writing to it, only one can use it any one time. This has the effect of making your parallel loop a synchronous one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900