Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithreading slower than Singlethreading
    primarykey
    data
    text
    <p>I have the following Code (complete content of 'Program.cs' of console application). The single threaded execution of 'countUp' till 'countUp4' takes 13 sec., the multi threaded execution 21 sec.. </p> <p>I have a Intel Core i5-2400 @ 3.10 GHz, 8 GB Ram, Windows 7 64 Bit. So why is the mutli threaded execution slower than the single threaded one?</p> <p>Is multithreading just useful for not blocking the main routine of simple c# applications? When does multithreading give me an advantage in execution speed?</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class Program { static int counter = 0; static int counter2 = 0; static int counter3 = 0; static int counter4 = 0; static void Main(string[] args) { Console.WriteLine("Without multithreading:"); Console.WriteLine("Start:" + DateTime.Now.ToString()); countUp(); countUp2(); countUp3(); countUp4(); Console.WriteLine(""); Console.WriteLine("With multithreading:"); Console.WriteLine("Start:" + DateTime.Now.ToString()); Thread thread1 = new Thread(new ThreadStart(countUp)); thread1.Start(); Thread thread2 = new Thread(new ThreadStart(countUp2)); thread2.Start(); Thread thread3 = new Thread(new ThreadStart(countUp3)); thread3.Start(); Thread thread4 = new Thread(new ThreadStart(countUp4)); thread4.Start(); Console.Read(); } static void countUp() { for (double i = 0; i &lt; 1000000000; i++) { counter++; } Console.WriteLine(counter.ToString()); Console.WriteLine(DateTime.Now.ToString()); } static void countUp2() { for (double i = 0; i &lt; 1000000000; i++) { counter2++; } Console.WriteLine(counter2.ToString()); Console.WriteLine(DateTime.Now.ToString()); } static void countUp3() { for (double i = 0; i &lt; 1000000000; i++) { counter3++; } Console.WriteLine(counter3.ToString()); Console.WriteLine(DateTime.Now.ToString()); } static void countUp4() { for (double i = 0; i &lt; 1000000000; i++) { counter4++; } Console.WriteLine(counter4.ToString()); Console.WriteLine(DateTime.Now.ToString()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload