Difference and speed comparison of three asynchronous methods of BeginInvoke, ThreadPool and task

Source: Internet
Author: User

Speed (Fastest 1) return value Multi-parameter Waiting to be completed within the time limit End after timeout
Threadpool.unsafequeueuserworkitem () 1 Non-native support 1 Non-native support Non-native support 3 Not supported
ThreadPool.QueueUserWorkItem () 2.7 Non-native support 1 Non-native support Non-native support 3 Not supported
Task () 4.5 Support 2 Non-native support Support Voluntary end
Delegate.beininvoke () 25.4 Non-native support 1 Support Support 4 Not supported
Thread.Start () 11009 Non-native support 1 Non-native support Non-native support 3 Support
  1. such as Threadpool.unsafequeueuserworkitem (() =>result=add (());

  2. With task<>

  3. Inside at the end of the program eventwaithandle.set (), Outside WaitOne (TimeSpan).

  4. Get The return value of BeginInvoke AsyncResult, then AsyncResult.AsyncWaitHandle.WaitOne ();


There is a picture of the truth. This is the time required for various asynchronous methods to loop n times.

The code is as follows:

     Static voidMain (string[] args) {Action ThreadStart= (() = { }); WaitCallback WaitCallback=NewWaitCallback (A = { }); Stopwatch Stopwatch=NewStopwatch ();            Stopwatch.reset ();            Stopwatch.start ();  for(inti =0; I <10000; i++) {System.Threading.ThreadPool.UnsafeQueueUserWorkItem (WaitCallback,NULL);            } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","Threadpool.unsafequeueuserworkitem ():", stopwatch.elapsedticks); Gc.            Collect ();            Stopwatch.reset ();            Stopwatch.start ();  for(inti =0; I <10000; i++) {System.Threading.ThreadPool.QueueUserWorkItem (waitcallback);            } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","ThreadPool.QueueUserWorkItem ():", stopwatch.elapsedticks); Gc.            Collect ();            Stopwatch.reset ();            Stopwatch.start ();  for(inti =0; I <10000; i++) {Task T=NewTask (ThreadStart);            T.start ();            } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","Task ():", stopwatch.elapsedticks); Gc.            Collect ();            Stopwatch.reset ();            Stopwatch.start ();  for(inti =0; I <10000; i++) {Threadstart.begininvoke (NULL,NULL);            } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","Delegate.beininvoke ():", stopwatch.elapsedticks); } 

Note that the usage of the above BeginInvoke is not complete and you should call EndInvoke again. But given that BeginInvoke is the slowest, the EndInvoke will not be added.

So, if there is no need to return a value, generally use ThreadPool bar, to more control, the task. I can't think of the time to use BeginInvoke.

Difference and speed comparison of three asynchronous methods of BeginInvoke, ThreadPool and task

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.