AsParallel 究竟是如何工作的?

发布于 2024-08-13 13:50:01 字数 668 浏览 12 评论 0原文

对于下面的测试程序来说,它似乎并没有做深蹲。这是因为我正在用一个小列表进行测试吗?

static void Main(string[] args)
{
    List<int> list = 0.UpTo(4);

    Test(list.AsParallel());
    Test(list);
}

private static void Test(IEnumerable<int> input)
{
    var timer = new Stopwatch();
    timer.Start();
    var size = input.Count();
    if (input.Where(IsOdd).Count() != size / 2)
        throw new Exception("Failed to count the odds");

    timer.Stop();
    Console.WriteLine("Tested " + size + " numbers in " + timer.Elapsed.TotalSeconds + " seconds");
}

private static bool IsOdd(int n)
{
    Thread.Sleep(1000);
    return n%2 == 1;
}

两个版本的运行时间均为 4 秒。

It doesn't seem to do squat for the following test program. Is this because I'm testing with a small list?

static void Main(string[] args)
{
    List<int> list = 0.UpTo(4);

    Test(list.AsParallel());
    Test(list);
}

private static void Test(IEnumerable<int> input)
{
    var timer = new Stopwatch();
    timer.Start();
    var size = input.Count();
    if (input.Where(IsOdd).Count() != size / 2)
        throw new Exception("Failed to count the odds");

    timer.Stop();
    Console.WriteLine("Tested " + size + " numbers in " + timer.Elapsed.TotalSeconds + " seconds");
}

private static bool IsOdd(int n)
{
    Thread.Sleep(1000);
    return n%2 == 1;
}

Both versions take 4 seconds to run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凯凯我们等你回来 2024-08-20 13:50:01

任务并行库关心序列的静态类型。对于要由 TPL 处理的操作,它应该是 IParallelEnumerable。当您调用 Test 时,您会将集合强制转换回 IEnumerable。因此,编译器会将序列上的 .Where 调用解析为 System.Linq.Enumerable.Where 扩展方法,而不是 TPL 提供的并行版本。

Task Parallel Library cares about the static type of the sequence. It should be IParallelEnumerable<T> for the operations to be handled by the TPL. You are casting the collection back to IEnumerable<T> when you call Test. Therefore, the compiler will resolve .Where call on the sequence to System.Linq.Enumerable.Where extension method instead of the parallel version provided by the TPL.

情深如许 2024-08-20 13:50:01

(更新.NET4,因为这个问题在 Google 搜索 AsParallel() 中排名相当靠前)

只需进行一些更改即可让您的示例按照我想象的方式工作。

更改 List; list = 0.UpTo(4);var list = Enumerable.Range(0, 4);

如果您添加了带有采用 ParallelQuery 签名的函数重载,则您的示例将有效...

    private static void Test(ParallelQuery<int> input)
    {
        var timer = new Stopwatch();
        timer.Start();

        int size = input.Count();
        if (input.Where(IsOdd).Count() != size / 2)
        {
            throw new Exception("Failed to count the odds");
        }
        timer.Stop();

        Console.WriteLine("Tested " + size + " numbers in " + timer.Elapsed.TotalSeconds + " seconds");
    }

或者,您可以使用 LINQ 语法...

    private static void Test(ParallelQuery<int> list)
    {
        var timer = Stopwatch.StartNew();

        if ((from n in list.AsParallel()
             where IsOdd(n)
             select n).Count() != (list.Count() / 2))
        {
            throw new Exception("Failed to count the odds");
        }

        Console.WriteLine("Tested " + list.Count() + " numbers in " + timer.Elapsed.TotalSeconds + " seconds");
    }

希望这对某人有帮助!

(Updating for .NET4 since this question ranks pretty high in a Google search for AsParallel())

Just a few changes will allow your example to work as I imagine you expected.

Change List<int> list = 0.UpTo(4); to var list = Enumerable.Range(0, 4);

Your example would work if you added a function overload with a signature that takes a ParallelQuery...

    private static void Test(ParallelQuery<int> input)
    {
        var timer = new Stopwatch();
        timer.Start();

        int size = input.Count();
        if (input.Where(IsOdd).Count() != size / 2)
        {
            throw new Exception("Failed to count the odds");
        }
        timer.Stop();

        Console.WriteLine("Tested " + size + " numbers in " + timer.Elapsed.TotalSeconds + " seconds");
    }

Alternatively, you could use LINQ syntax....

    private static void Test(ParallelQuery<int> list)
    {
        var timer = Stopwatch.StartNew();

        if ((from n in list.AsParallel()
             where IsOdd(n)
             select n).Count() != (list.Count() / 2))
        {
            throw new Exception("Failed to count the odds");
        }

        Console.WriteLine("Tested " + list.Count() + " numbers in " + timer.Elapsed.TotalSeconds + " seconds");
    }

Hope this helps someone!

猫烠⑼条掵仅有一顆心 2024-08-20 13:50:01

并行的工作原理是将你的东西放入线程池中。另外,你有多少个核心?如果您正在使用单核机器,则仍然需要大约 4 秒才能运行。

As Parallel works by putting your stuff into the ThreadPool. Also, how many cores do you have? If you're working on a single core machine that will still take about 4s to run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文