Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi guys,
Since I'm concerned about speed and efficiency, I began reading Gastón C. Hillar's Professional Parallel Programming with C# book and want to rewrite my project from the ground up using the parallel programming approach.
In the following exercise, I noticed that not all steps of the parallel loop are executed while comparing the execution speed of the sequential and parallel loops.
I noticed this by analyzing the text length.
On the other hand, if I also use lock, Parallel Programming becomes completely pointless.
In the sequential loop, the length is always 200,000, but in the parallel loop it is variable.
Sequential loop
C#
System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();
StringBuilder SB = new StringBuilder();
Console.ReadKey();
StopWatch.Start();
for (int i = 0; i < 200000; i++)
{
    SB.Append("B");
}
StopWatch.Stop();
Console.WriteLine("Total seconds: " + StopWatch.Elapsed.TotalSeconds + ", Length: " + SB.Length);
Console.ReadKey();

Result:
Click on this link to see the result

Parallel.For
C#
System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();
StringBuilder SB = new StringBuilder();
Console.ReadKey();
StopWatch.Start();
Parallel.For(0, 200000, range =>
{
    SB.Append("B");
});
StopWatch.Stop();
Console.WriteLine("Total seconds: " + StopWatch.Elapsed.TotalSeconds + ", Length: " + SB.Length);
Console.ReadKey();

Results:
One of the results (click to see)

Parallel.ForEach
C#
System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();
StringBuilder SB = new StringBuilder();
Console.ReadKey();
StopWatch.Start();
Parallel.ForEach(Partitioner.Create(0, 200000, ((200000 / Environment.ProcessorCount) + 1)), range =>
{
    for (int i = range.Item1; i < range.Item2; i++)
    {
        SB.Append("B");
    }
});
StopWatch.Stop();
Console.WriteLine("Total seconds: " + StopWatch.Elapsed.TotalSeconds + ", Length: " + SB.Length);
Console.ReadKey();

Results:
One of the results (click to see)


1.What is the reason for this, and how can I know if the entire parallel loop has been implemented in the projects? Please explain with an example.
2.Also, what exactly is the concept of overhead?

I use the following tools:
.NET Framework 4.5
Console app

Thank you for your time.
Best regards

What I have tried:

I also tried List<> as shown below; however, the List.Count is once again variable. Lastly, I was unable to utilize this parallel programming because of my lack of understanding.
C#
System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();
List<object> List = new List<object>();
Console.ReadKey();
StopWatch.Start();
Parallel.For(0, 200000, range =>
{
    List.Add("B");
});
StopWatch.Stop();
Console.WriteLine("Total seconds: " + StopWatch.Elapsed.TotalSeconds + ", Length: " + List.Count);
Console.ReadKey();
Posted
Updated 6-Sep-22 4:52am
v2
Comments
George Swan 6-Sep-22 15:03pm    
My experience is that StopWatch is just not accurate enough for any meaningful performance comparisons to be made. I would recommend that you use BenchmarkDotNet
Reza jafery 7-Sep-22 11:06am    
For the time being, I consider this question open, that is, without an answer, so that I can do additional study and provide a more full response.
Reza jafery 7-Sep-22 11:07am    
Of course, if someone provides a comprehensive response, it will be accepted.
Reza jafery 7-Sep-22 11:11am    
I thank you all for your recent replies.

You cannot just call any method on any object you want in a parallel fashion. None of the StringBuilder methods can be called from multiple threads at the same time and have your output make any sense.

The List class is also one of those classes that is not "thread safe". You cannot call it's methods from multiple threads at the same time.

It would probably be a good idea if you read up on Thread-Safe Collections | Microsoft Docs[^].


Oh, and you cannot just throw a Parallel anything at a problem and expect it to work. The problem you're to solve must be able to be solved in a parallel fashion. That means every item in a list of items you're doing processing on can NOT depend on the values or state of previous items in your list.

For example, would it makes sense for a StringBuilder to support adding text to its buffer from multiple threads simultaneously? No! You would get scrambled text in the result, and what appears to be missing text because more than one thread CAN write to the StringBuilder at the same position in its buffer from multiple threads at the exact same time!
 
Share this answer
 
v2
Comments
Reza jafery 6-Sep-22 13:45pm    
Thank you for your response. I read the article.
Could you list all the items that are designed for parallel processing, such as collections, classes, and...?
Dave Kreskowiak 6-Sep-22 15:29pm    
You will have to read the documentation on whatever classes you're thinking of using. Nobody is going to put a list of thousands of classes together for you.

See the notes at Parallel Loops | Microsoft Docs[^].
 
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