c# - Parallel always seems slower while calculating prime -


It's probably not strange, it's really nothing that I do not really understand. I was just experimenting with parallel programming and I thought I could implement an example of such simplicity that might be similar; The prime minister's calculation

The point is: I can not get the only 4 logical processors to calculate the prime minister when I thread it apart. Why is it like this? (I have an i7-4500u)

Here is my code (you can paste it originally in a new console app):

  Fixed zero main (string [] Arg) {var p = new program (); P.Start (); } Private Zero Starter () {StartMonitoringTask (); // It keeps my CPU at 33%, but for really fast (long time current = 3; current & lt; long maximum value; current ++) {set value and temporarily (current); } // It keeps my CPU at 100%, but the way slow is I parallel. For (3, long. Maxwell, (current) = & gt; fixed value and temporarily (current)); } Private long last light = 0; Private long-length = 0; Determine Private ZerosTerms and Adotals (Long Prime or Not) {bool isPrime = true; If (primer note% 2 == 0) returns; // Even number? Prime Minister never long Route = (tall) Math.Sqrt ((Long) Prime or Not); // Check for only unequal numbers for (Int i = 3; i & lt; = root; i + = 2). {If (principal or not% i == 0) {isPrime = false; break; }} If {IsPrime} {totalFound ++; Final Light = Principal Onnot; }} /// & lt; Summary & gt; /// It just starts a task to monitor progress. /// It outputs results to every other console either. /// & lt; / Summary & gt; Private Zero StartMonitoringTask () {Tasks. Factor Start New ((=) = & gt; {var sw = Stopwatch} Startup ()} while (true) {Task .Delay (1000). Wait (); console.lightline ("found:" + TotalFound + ", last: "+ Last Light +", "+ (Kulfound / (sw.Elapsed milliseconds / 1000)) +" P / S "}}}, Cancellation token.None, Functioning options. Langaran, Task Scheduler. Default;}  

UPDATE (based on Fried's answer): Fraud's answer seems to be, so to prove this I have parallel Adding the form to a parallel, many actions are called as:

  var numberof = 20; var verb = new list & lt; verb & gt; (); long chocisees = int For Mac OS X (for example, from = I == 0? 3: (i * Chunk size); = (I + 1) * Chunk size; Actions.Add (New Action ((=) = & gt; {Determine the length of J * L;;; J ++) Define the method and the AdTocal (J);}));} Parallel. Invoke (verb tuare ());  

It seems slow in the form of parallel. However, what am I missing?

The cost of spinning new threads for each integer value in the int64-range is very expensive. Split the 64-category gap into

, divide the 10 sections, and in turn execute it for each segment by using parallel.Invoke (verb [] verb.

Then you definitely see the display profit.

  parallel. For invoke ((=) => (int i = 3; i & lt; a; i ++ ) Define the order and the adapter (i);}, () = & gt; {for (int i = a; i & lt; b; i ++), determine the order and adapter (i);}, () => For (int i = b; i & lt; c; i ++) prime and endotle (i);}, determination The book ..  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -