Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why so much difference in performance between Thread and Task?

Windows 7, Intel CORE i3, 64 bit, RAM 4Gb, 2.27 GHz
.NET Framework 4.0

I have the following code:

static void Main(string[] args) {     var timer = new Stopwatch();     timer.Start();      for (int i = 0; i < 0xFFF; ++i)     {         // I use one of the following line at time         Task.Factory.StartNew(() => { });         new Thread(() => { }).Start();     }      timer.Stop();      Console.WriteLine(timer.Elapsed.TotalSeconds);     Console.ReadLine(); } 

If I use Task the output is always less then 0.01 seconds, but if I use Thread the output is always greater than 40 seconds!
How is it possible? Why so much difference?

like image 342
Nick Avatar asked Oct 29 '12 15:10

Nick


People also ask

Why are tasks better than threads?

It is always advised to use tasks instead of thread as it is created on the thread pool which has already system created threads to improve the performance. The task can return a result. There is no direct mechanism to return the result from a thread. Task supports cancellation through the use of cancellation tokens.

What is the difference between task vs thread which one is better?

Differences Between Task And ThreadThe Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel. The task can return a result.

What is the difference between process thread and task?

The difference between a thread and a process is, when the CPU switches from one process to another the current information needs to be saved in Process Descriptor and load the information of a new process. Switching from one thread to another is simple. A task is simply a set of instructions loaded into the memory.

What is the difference between thread and task in RTOS?

The threads within a process have their own context, exactly the same as task in the RTOS. The difference being that a thread is running in a virtual address space, whereas a task runs in a physical one.

What is the difference between task and thread?

Task uses a queue, so its much faster to create a Task than a Thread. I'll bet that even if you waited for Tasks/Threads to finish, that using a Task is faster. The overhead of creating and then destroying a Thread is high. That's why the Task.Factory was created!

What is the difference between a task and a ThreadPool?

A threadpool is.. a pool of threads, which are ready to carry out instructions (if they are not occupied of course). You can see a Task as a higher level abstraction of threads, which could be a reason why they are under the System.Threading namespace. You can accomplish the same with a thread as you can with a task.

How does threading affect CPU performance?

Each thread you have consumes a non-trivial amount of memory for its stack, and adds additional CPU overhead as the processor context-switchbetween threads. Instead, it is better to have a small pool of threads execute your code as work becomes available.

What are the benefits of a separate thread for each task?

This causes the overhead of scheduling the tasks to be far lower, as the threads don't have to be created each iteration. Note that the behavior is not the same, however. When creating a separate thread, each task is getting it's own thread. They will all get started right away.


1 Answers

The two are not the same.

When you use Task.Factory.StartNew, you're scheduling a task to run on the ThreadPool. When you make a new Thread, you're having to create and start a new thread.

In the first case, the threads are already created and reused. This causes the overhead of scheduling the tasks to be far lower, as the threads don't have to be created each iteration.

Note that the behavior is not the same, however. When creating a separate thread, each task is getting it's own thread. They will all get started right away. When using Task.Factory.StartNew, they're put into the scheduler to run on the ThreadPool, which will (potentially) limit the number of concurrent threads started. This is usually a good thing, as it prevents overthreading from occurring.

like image 184
Reed Copsey Avatar answered Sep 18 '22 14:09

Reed Copsey