简体   繁体   English

C#使用Task无法获得性能提升吗?

[英]C# No performance gain with Task?

I used Task like below but there is no performance gain. 我像下面一样使用Task,但是没有性能提升。 I checked my method which executes in 0-1 seconds but with Task(30 Tasks), it takes 5-12 seconds. 我检查了我的方法,该方法在0-1秒内执行,但是使用Task(30 Tasks),需要5-12秒。 Can anyone guide if I have done any mistake. 如果我做错了任何人都可以指导。 I want to run 30 parallel and expect 30 done in max 2 seconds. 我想并行运行30个,并希望在最多2秒内完成30个。

Here is my code: 这是我的代码:

Task[] tasks = new Task[30];
for (int p = 0; p <= dstable.Tables[0].Rows.Count - 1; p++)
{
    MethodParameters newParameter = new MethodParameters();
    newParameter.Name = dstable.Tables[0].Rows[p]["Name"].ToString();

    tasks[p] = Task.Factory.StartNew(() => ParseUri(newParameter));
    Application.DoEvents();
}
try
{
    Task.WaitAll(tasks);
    //Console.Write("task completed");
}
catch (AggregateException ae)
{
    throw ae.Flatten();
}

There are some major problems in your thinking. 您的思考中存在一些主要问题。

  1. does your PC have 30 Cores, so that every core can exactly takes one task? 您的PC是否具有30个内核,以便每个内核可以完全执行一项任务? I don't think so 我不这么认为
  2. starting a seperate thread also takes some time. 启动单独的线程也需要一些时间。
  3. with every concurrent thread more overhead is generated. 每个并发线程都会产生更多开销。
  4. Can your problem be solved faster by starting more threads? 通过启动更多线程可以更快地解决您的问题吗? This is only the case, when all threads do different tasks, like reading from disk, quering a database, computing something, etc.. 10 threads that do all "high-performance" tasks on the cpu, won't give an boost, quite contrary to, because every thread needs to clean up his mess, before he can give some cpu time to the next thread, and that one needs to clean up his mess too. 只有当所有线程都执行不同的任务(例如从磁盘读取,查询数据库,计算内容等)时,情况才如此。10个在CPU上执行所有“高性能”任务的线程不会提供任何帮助,完全相反,因为每个线程都需要清理混乱,才可以给下一个线程一些cpu时间,而那个线程也需要清理混乱。

Check this link out http://msdn.microsoft.com/en-us/library/ms810437.aspx 检查此链接http://msdn.microsoft.com/en-us/library/ms810437.aspx

You can use the TPL http://msdn.microsoft.com/en-us/library/dd460717.aspx 您可以使用TPL http://msdn.microsoft.com/en-us/library/dd460717.aspx

they try to guaranty the maximum effect from parallel threads. 他们试图保证并行线程的最大效果。 Also I recommend this book http://www.amazon.com/The-Multiprocessor-Programming-Maurice-Herlihy/dp/0123705916 我也推荐这本书http://www.amazon.com/The-Multiprocessor-Programming-Maurice-Herlihy/dp/0123705916

When you really want to solve your problem in under 2 seconds, buy more CPU power ;) 当您真的想在2秒内解决问题时,请购买更多的CPU电源;)

I think you may have missing the main point of using thread. 我认为您可能缺少使用线程的要点。

  • Creating many threads may lead to more complexity by increasing OS over-head. 创建许多线程可能会增加操作系统开销,从而导致更高的复杂性。 (Context switching) (上下文切换)
  • Also it will be much harder to manage your execution of code and harder to find and fix bugs if there is any. 此外,管理代码的执行将变得更加困难,并且如果存在错误,将更加难以发现和修复错误。

Usage of thread may give you advantage when there is 线程的使用可能会给您带来好处

  • Task need to be perform simultaneously 任务需要同时执行
  • Building responsive UI 构建响应式用户界面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM