c# - How can I determine whether a parallel foreach loop is going to have better performance than a foreach loop? -


i did simple test in .net fiddle of sorting 100 random integer arrays of length 1000 , seeing whether doing paralell.foreach loop faster plain old foreach loop.

here code (i put fast, please ignore repetition , overall bad of code)

using system; using system.net; using system.collections.generic; using system.threading; using system.threading.tasks; using system.linq;  public class program {     public static int[] randomarray(int minval, int maxval, int arrsize)     {         random randnum = new random();         int[] rand = enumerable             .repeat(0, arrsize)             .select(i => randnum.next(minval, maxval))             .toarray();          return rand;     }      public static void sortonethousandarrayssync()     {         var arrs = new list<int[]>(100);         for(int = 0; < 100; ++i)             arrs.add(randomarray(int32.minvalue,int32.maxvalue,1000));         parallel.foreach(arrs, (arr) =>         {             array.sort(arr);         });     }      public static void sortonethousandarraysasync()     {         var arrs = new list<int[]>(100);         for(int = 0; < 100; ++i)             arrs.add(randomarray(int32.minvalue,int32.maxvalue,1000));         foreach(var arr in arrs)         {             array.sort(arr);         };           }      public static void main()     {         var start = datetime.now;         sortonethousandarrayssync();         var end = datetime.now;         console.writeline("t1 = " + (end - start).tostring());         start = datetime.now;         sortonethousandarraysasync();         end = datetime.now;         console.writeline("t2 = " + (end - start).tostring());     } } 

and here results after hitting run twice:

t1 = 00:00:00.0156244 t2 = 00:00:00.0156243 

...

t1 = 00:00:00.0467854 t2 = 00:00:00.0156246 

...

so, it's faster , it's same.

possible explanations:

  • the random arrays "more unsorted" sync 1 versus async 1 in 2nd test ran
  • it has processes running on .net fiddle. in first case parallel 1 ran non-parallel operation because there weren't threads fiddle take over. (or that)

thoughts?

you should use parallel.foreach() if code within loop takes significant amount of time execute. in case, takes more time create multiple threads, sort array, , combine result onto 1 thread sort on single thread. example, parallel.foreach() in following code snippet takes less time execute normal foreach loop:

public static void main(string[] args) {     var numbers = enumerable.range(1, 10000);      parallel.foreach(numbers, n => factorial(n));      foreach (var number in numbers)     {         factorial(number);     } }  private static int factorial(int number) {     if (number == 1 || number == 0)         return 1;      return number * factorial(number - 1); } 

however, if change var numbers = enumerable.range(1, 10000); var numbers = enumerable.range(1, 1000);, foreach loop faster parallel.foreach().


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -