c# - AsParallel() or async/await -


Assume that a method to CountString , the string and an integer array are given The length of the strings is greater than that of int if I have to take advantage of multicore hardware as much as possible, it is enough to do this:

  public int countstring (string [] s, int i) { Return s.asparallel () Calculation (race => res.length & gt; i); } Is  

or do I have to use some kind of work or work and plumber get mixed up?

It should be considered just a simple example, I know this way does not affect hardware performance very much.

I am thinking that it is better to do this, using AsParallel () , or if it is better to declare the method async and Use the waiting in the body of the method (even if I do not know).

AsParallel will be a good way to go, because in fact nothing does not require you to await since you are working with the collection, PLINQ or Paralle.ForEach is a good option. Async I / O is inherently operational, then when going with async-await


If you actually test your code, you may be surprised to see that this piece of code actually exposes your negative display cost On the basis of the system's execution on the basis of size , you are turning it on again.

Many times people forget that to actually use the thread Overhead, even when using thread threads. You have at least cp Procedures should be intensive work, it will be good to hit the performance of your parallel.

If your array is quite long, so AsParallel should use enough. There is no reason to add Task because PLINQ will take care of consolidation only.

OK, it helps to test the code. I'm going to run again with GUIDs on the string [] . This code is:

Main method:

  Zero main () {// JIT test (0); Test (100); Test (1000); Test (10000); Test (1000000); Test (10000000); } Public Zero Test (int item amount) {string [] strings = energetic. Range (0, item amount). Select (i = & gt; Guid.NewGuid () .ostrowing ()). Toure (); Var stopwatch = stopwatch Startup (); Count String Inparle (Strings, MadAmain); StopWatch.Stop (); Console.lightline ("parallel call: string amount: {0}, time: {1}", itemarm, stopwatch. Applepe); StopWatch.Restart (); Calculation String Sinks (Strings, MadAmain); StopWatch.Stop (); Console.light line ("synchronous call: string amount: {0}, time: {1}", item amount, stopwatch. Applepe); }  

Parallel and sync:

  public int CountStringInParallel (string [] s, int i) {return s.AsParallel (). Calculation (res = & gt; res.Length & gt; i); } Public Integer Count String Sync (string [] s, int i) {return s. Corset (race => res.Length & gt; i); }  

Result:

Parallel call: string amount: 100, time: 00: 00: 00.0000197

synchronous call: string amount: 100, time: 00: 00: 00.0000026


parallel call: string amount: 1000, Time: 00: 00: 00.0000266

Synchronous call: string amount: 1000, time: 00: 00: 00.0000201

strong> 00: 00: 00.0002003


parallel call: string Amount: 1000000, Time: 00: 00: 00.0080492 00: 00: 00.0135279


parallel call Synchronous Ous call: string amount: 10000000, time: 00: 00: 00.1402474 : string amount: 10000000, time: 00: 00: 00.0744104

You can see that up to 10,000 stars, the synchronous method is really fast parallel.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -