c# - PLINQ Performs Worse Than Usual LINQ -
Surprisingly, using PLINQ I have not earned profit on a small test case; In fact, it was even worse than the normal LINQ.
Here is the test code:
int repeated count = 10000000; Private Zero Button 1_Click (Object Sender, EventArgs e) {var CurrTime = DateTime.Now; Var strList = Exclusive. Repetition (10, repeated); Var result = strList.AsParallel (). Yoga (); Var currTime2 = DateTime.Now; Textbox1.Text = (curtTime2.Ticks-curtTime.Ticks) .restring (); } Private Zero Button 2_Click (Object Sender, EventArgs e) {var CurrTime = DateTime.Now; Var strList = Exclusive. Repetition (10, repeated); Var result = strList.Sum (); Var currTime2 = DateTime.Now; Textbox2.Text = (curtTime2.Ticks - curtTime.Ticks) .restring (); }
The result?
So, it is taking less time than LINQ to complete PLINQ for a similar operation!
Am I doing wrong? Or is there a turn about which I do not know?
Edit: To use a stopwatch, I have updated my code, and still, the same behavior remains. To get rid of GIT's effect, I actually tried many times with the click of button1
and button2
and in a particular order, although I can have a different time But the qualitative behavior persists: PLINQ was actually slow in this case.
First: to measure runtime Stop using Datetime for use Use a stopwatch instead The test code will look like this:
var clock = new stopwatch (); Var strList = Noteworthy Repetition (10, 10000000); Watch.Start (); Var result = strList.Sum (); Watch.Stop (); Console.light line ("linear: {0}", clock. Alepsed milliseconds); Watch.Reset (); Watch.Start (); Var parallel sage = strList.AsParallel (). Yoga (); Watch.Stop (); Console.lightline ("parallel: {0}", clock. Apple milliseconds); Console.ReadKey ();
Second: Things that run in parallel combine overhead. In this case, PLINQ should know the best way to split your archive so that it is in parallel Could add elements safely. After this, you have to join the results of various threads and add them. This is not a trivial task.
Using the code given above, I can see that ~ 95 ms call using the sum (). Calling .AsParallel () .net () trap ~ around 185ms.
Performing one task in parallel is just a good idea if you do something by doing it. In this case, yoga is a simple task that you do not get by using PLINQ.
Comments
Post a Comment