Task.WaitAll法VS Parallel.Invoke方法 [英] Task.WaitAll method vs Parallel.Invoke method

查看:1278
本文介绍了Task.WaitAll法VS Parallel.Invoke方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的示例代码进行比较处理时间并行的办法和任务的方法。这个实验的目的是它们如何工作理解

I have sample code to compare processing time for Parallel approach and Task approach. The goal of this experiment is understanding of how do they work.

所以我的问题是:


  1. 为什么并行工作的速度,然后任务?

  2. 请我的结果意味着,我应该使用并行的,而不是任务?

  3. 我应该在哪里使用任务并在并行?

  4. 什么比较使用任务的好处水货?

  5. 请问任务仅仅是ThreadPool的一个总结。 ?QueueUserWorkItem方法

  1. Why Parallel worked faster then Task?
  2. Do my results mean that I should use Parallel instead of Task?
  3. Where should I use Task and where Parallel?
  4. What benefits of using Task in comparison to Parallel?
  5. Does Task is just a wrap for ThreadPool.QueueUserWorkItem method?

    public Task SomeLongOperation()
    {
        return Task.Delay(3000);
    }

    static void Main(string[] args)
    {
        Program p = new Program();
        List<Task> tasks = new List<Task>();

        tasks.Add(Task.Factory.StartNew(() => p.SomeLongOperation()));
        tasks.Add(Task.Factory.StartNew(() => p.SomeLongOperation()));

        var arr = tasks.ToArray();

        Stopwatch sw = Stopwatch.StartNew();
        Task.WaitAll(arr);
        Console.WriteLine("Task wait all results: " + sw.Elapsed);
        sw.Stop();

        sw = Stopwatch.StartNew();
        Parallel.Invoke(() => p.SomeLongOperation(), () => p.SomeLongOperation());
        Console.WriteLine("Parallel invoke results: " + sw.Elapsed);
        sw.Stop();

        Console.ReadKey();
    }


下面是我的处理结果:

Here are my processing results:

编辑:

更改代码,如下所示:

    Program p = new Program();
    Task[] tasks = new Task[2];

    Stopwatch sw = Stopwatch.StartNew();
    tasks[0] = Task.Factory.StartNew(() => p.SomeLongOperation());
    tasks[1] = Task.Factory.StartNew(() => p.SomeLongOperation());

    Task.WaitAll(tasks);
    Console.WriteLine("Task wait all results: " + sw.Elapsed);
    sw.Stop();

    sw = Stopwatch.StartNew();
    Parallel.Invoke(() => p.SomeLongOperation(), () => p.SomeLongOperation());
    Console.WriteLine("Parallel invoke results: " + sw.Elapsed);
    sw.Stop();

我的新成果:

编辑2:
当我换成代码Parallel.Invoke是第一和Task.WaitAll被秒的情况已经改变cardinally。现在并行较慢。这让我想起我的估计不正确的。我改变了代码,如下所示:

EDIT 2: When I replaced code with Parallel.Invoke to be first and Task.WaitAll to be second the situation has been changed cardinally. Now Parallel is slower. It makes me think of incorrectness of my estimates. I changed code to look like this:

Program p = new Program();
Task[] tasks = new Task[2];

Stopwatch sw = null;
for (int i = 0; i < 10; i++)
{
    sw = Stopwatch.StartNew();
    Parallel.Invoke(() => p.SomeLongOperation(), () => p.SomeLongOperation());
    string res = sw.Elapsed.ToString();
    Console.WriteLine("Parallel invoke results: " + res);
    sw.Stop();
}

for (int i = 0; i < 10; i++)
{
    sw = Stopwatch.StartNew();
    tasks[0] = Task.Factory.StartNew(() => p.SomeLongOperation());
    tasks[1] = Task.Factory.StartNew(() => p.SomeLongOperation());
    Task.WaitAll(tasks);
    string res2 = sw.Elapsed.ToString();
    Console.WriteLine("Task wait all results: " + res2);
    sw.Stop();
}

这是我的新成果:

现在我可以建议这个实验是更清晰。该结果几乎是相同的。有时平行,有时任务更快。现在,我的问题是:

Now I can suggest that this experiment is much more clear. The results are almost the same. Sometimes Parallel and sometimes Task is faster. Now my questions are:

1。我应该在哪里使用任务并在并行?

2。什么比较使用任务到并行的好处是什么?

3。请问任务仅仅是ThreadPool.QueueUserWorkItem方法的总结?

任何有用的信息,可以澄清这些问题是值得欢迎的。

Any helpful info that can clarify those questions are welcome.

推荐答案

作为编辑的通过 MSDN 这篇文章:

EDIT as of this article from MSDN:

并行和任务是线程池包装。 。并行调用也在等待着,直到所有任务将完成

Both Parallel and Task are wrappers for ThreadPool. Parallel invoke also awaits until all tasks will be finished.

有关您的问题:

使用任务,并行或线程池取决于你需要对你的并行任务执行控制的粒度。我个人习惯了 Task.Factory.StartNew(),但是这是一个个人的看法。同样涉及到 ThreadPool.QueueUserWorkItem()

Using Task, Parallel or ThreadPool depends on the granularity of control you need to have on the execution of your parallel tasks. I'm personally got used to Task.Factory.StartNew(), but that's a personal opinion. The same relates to ThreadPool.QueueUserWorkItem()

附加信息:要Parallel.Invoke的第一次调用()和Task.Factory.StartNew()可能由于内部初始化慢一些。

Additional Information: The first call to Parallel.Invoke() and Task.Factory.StartNew() might be slower due to internal initialization.

这篇关于Task.WaitAll法VS Parallel.Invoke方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆