Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my multi-threading slower than my single threading?

I know a couple of people asked a question similar to this, but I can’t find any response that would make me understand why it's slower.

So, I made a little console program for my own understanding of the threading object in Visual Studio 2013. My CPU is an Intel Core i7 that supply can use multiple threading.

My code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Diagnostics;  namespace ConsoleApplication1 {     class Program     {          static TimeSpan MTTime;         static TimeSpan STTime;          static void Main(string[] args)         {             Stopwatch stopwatch = new Stopwatch();             stopwatch.Start();               Console.WriteLine(Environment.NewLine + "---------------Multi Process-------------" + Environment.NewLine);              Thread th1 = new Thread(new ParameterizedThreadStart(Process));             Thread th2 = new Thread(new ParameterizedThreadStart(Process));             Thread th3 = new Thread(new ParameterizedThreadStart(Process));             Thread th4 = new Thread(new ParameterizedThreadStart(Process));              th1.Start("A");             th2.Start("B");             th3.Start("C");             th4.Start("D");              th1.Join();             th2.Join();             th3.Join();             th4.Join();              stopwatch.Stop();             MTTime = stopwatch.Elapsed ;              Console.WriteLine(Environment.NewLine + "---------------Single Process-------------" + Environment.NewLine);               stopwatch.Reset();             stopwatch.Start();              Process("A");             Process("B");             Process("C");             Process("D");              stopwatch.Stop();             STTime = stopwatch.Elapsed;              Console.Write(Environment.NewLine + Environment.NewLine + "Multi  : "+ MTTime + Environment.NewLine + "Single : " + STTime);               Console.ReadKey();         }          static void Process(object procName)         {             for (int i = 0; i < 100; i++)             {                 Console.Write(procName);             }         }     } } 

Result image:

enter image description here

We can clearly see that the multi-treading process is total random and the single one just do all presses on after the other, but I don't think this have an impact on the speed.

At first, I thought my thread was just bigger than the process needed for running the program, but after changing for a bigger process the single treading still was still the fastest in a big way. So, do i miss a concept in multi-threading? Or it normal that is slower?

like image 584
Kazimar Avatar asked Apr 30 '15 06:04

Kazimar


People also ask

Why is multithreading slow?

This depends rather on how many CPUs your code gets given to run on by the OS. Each of these threads is CPU bound so if you have just the one CPU it's going to run one thread for a bit, timeslice it, run the next thread, etc, which won't be any faster and may well be slower, depending on the overhead of a thread swap.

Is single thread faster than multi thread?

On a single core CPU, a single process (no separate threads) is usually faster than any threading done. Threads do not magically make your CPU go any faster, it just means extra work.

Why using more threads makes it slower than using less threads?

Switching between threads has a cost. Synchronization between threads has a cost. Adding threads is not a magical silver bullet that will speed things up. If there is not enough parallelism in the problem to overcome the overhead of using threads, then you may very well slow down your code by adding more threads.

Will a multithreaded solution always provide better performance than a single threaded solution?

They do not make the computer run faster. All they can do is increase the efficiency of the computer by using time that would otherwise be wasted. In certain types of processing this optimization can increase efficiency and decrease running time.


Video Answer


2 Answers

Note that Process writes to the console (and basically does nothing else), and that output to the console (which here acts as a kind of shared resource) is slow and needs to be synchronized with the other threads.

To my understanding, the parallelization you use creates a huge overhead but gains no speedup, because all of the time the threads are apparently mostly waiting for the other process to finish writing to the console.

like image 92
Codor Avatar answered Sep 18 '22 14:09

Codor


From the official Console documentation

I/O operations that use these streams are synchronized, which means that multiple threads can read from, or write to, the streams. This means that methods that are ordinarily asynchronous, such as TextReader.ReadLineAsync, execute synchronously if the object represents a console stream

This means that the console class handles the thread synchronization so if thread A and thread B are trying to write to the console the console will handle them and only one for time will be able to write. The handling logic behind it is the reason why it takes longer


UPDATE
I suggest you to have a look at Parallel.ForEach

like image 29
Sid Avatar answered Sep 19 '22 14:09

Sid