c# - Comparison time- Single Threads vs Multiple Threads differing in results -


i wrote code in purpose test multi , single threading speeds. feedback! rewrote of based on great comments received. functions (maybe has bug here or there), tests multi threads first, , takes average find more accurate speed: (scroll bottom cont.)

main method class

using system;  namespace singleandmultithreading { internal class threads {     private static void main(string[] args)     {          long numofobjcreated;         int numberoftests;          while (true)         {             try             {                 console.write("number of objects create: ");                 numofobjcreated = convert.toint64(console.readline());                 break;             }             catch (exception)             {                 console.writeline("invalid input.");             }         }           while (true)         {             try             {                 console.write("number of tests run: ");                 numberoftests = convert.toint32(console.readline());                 break;             }             catch (exception)             {                 console.writeline("invalid input.");             }         }           calculateresults(numofobjcreated, numberoftests);          console.readkey();      }       private static void calculateresults(long numofobjcreated, int numberoftests)     {         double totalpercentages = 0;         (var = 0; < numberoftests; i++)         {             totalpercentages += completetests(numofobjcreated);         }          var accuracy = totalpercentages / numberoftests;          if ((int)accuracy == 0)         {             console.writeline("\nin case, neither single threading or multithreading faster.\n" +                               "they both run equally under these conditions.\n");             return;         }          if (accuracy < 0)         {             console.writeline("\nin case {0} objects being created, single threading faster!\n",                 string.format("{0:#,###0}", numofobjcreated));             return;         }          console.writeline("\nfrom {0} test(s), {1}% average percentage of increased speed in multithreading.\n",             string.format("{0:#,###0}", numberoftests), string.format("{0:#,###0}", accuracy));     }      private static double completetests(long numofobjcreated)     {         console.writeline("computing...");          var numofcores = environment.processorcount;          var timeformultithread = multithread.run(numofobjcreated, numofcores);         var timeforsinglethread = singlethread.run(numofobjcreated);          var percentfaster = ((timeforsinglethread / timeformultithread) * 100) - 100;          //note: .net part in assigning certian thread own core          console.writeline("using {0} cores, creating {1} objects {2}% faster.",             numofcores, string.format("{0:#,###0}", numofobjcreated), string.format("{0:#,###0}", percentfaster));          return percentfaster;     } } } 

single threading class

using system; using system.diagnostics;  namespace singleandmultithreading { internal class singlethread {     public static double run(long numofobjcreated)     {         var watch = new stopwatch();          watch.start();          (long = 0; < numofobjcreated; i++)         {             new object();         }          watch.stop();          var totaltime = watch.elapsedticks;          console.writeline("the time create {0} objects 1 thread is: {1} ticks.",             string.format("{0:#,###0}", numofobjcreated), string.format("{0:#,###0}", totaltime));          return totaltime;      } } } 

multi threading class

using system; using system.collections.generic; using system.diagnostics; using system.threading;  namespace singleandmultithreading { internal class multithread {     public static double run(long numofobjcreated, int numofcores)     {         var watch = new stopwatch();          var workerobject = new worker(numofobjcreated / numofcores);          var listofthreads = new list<thread>();           (long k = 0; k < numofcores; k++)         {             var workerthread = new thread(workerobject.dowork);             listofthreads.add(workerthread);         }          watch.start();         foreach (var thread in listofthreads)         {             thread.start();         }          byte countofcompletedthreads = 0;          while (true)         {             foreach (var thread in listofthreads)                 if (!thread.isalive)                     countofcompletedthreads++;              if (countofcompletedthreads == numofcores)                 break;             countofcompletedthreads = 0;          }          watch.stop();          var totaltime = watch.elapsedticks;          console.writeline("the time create {0} objects utilizing {1} cores is: {2} ticks.",             string.format("{0:#,###0}", numofobjcreated), numofcores, string.format("{0:#,###0}", totaltime));          return totaltime;      } } } 

worker class

namespace singleandmultithreading { public class worker {     private readonly long _numofobjtocreate;     public bool isdone;      public worker(long numofobjtocreate)     {         _numofobjtocreate = numofobjtocreate;     }      public void dowork()     {         (long = 0; < _numofobjtocreate; i++)         {             new object();         }          isdone = true;     } } } 

the output of code bit long post (i urge copy , paste own ide, fascinating). guess accepted answer doesn't give same result per every test due cpu scheduling, other or minor issues aslr , such. more 1 thing happening aside visual studio running program, , priorities differently. thank pointing out running multi threading first helps because of already-done memory allocation!

another thing point out, found while running:

analysis

the spikes when process of multi threading takes place.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -