AsParallel 究竟是如何工作的?
对于下面的测试程序来说,它似乎并没有做深蹲。这是因为我正在用一个小列表进行测试吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
任务并行库关心序列的静态类型。对于要由 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 toIEnumerable<T>
when you callTest
. Therefore, the compiler will resolve.Where
call on the sequence toSystem.Linq.Enumerable.Where
extension method instead of the parallel version provided by the TPL.(更新.NET4,因为这个问题在 Google 搜索 AsParallel() 中排名相当靠前)
只需进行一些更改即可让您的示例按照我想象的方式工作。
更改
List; list = 0.UpTo(4);
到var list = Enumerable.Range(0, 4);
如果您添加了带有采用 ParallelQuery 签名的函数重载,则您的示例将有效...
或者,您可以使用 LINQ 语法...
希望这对某人有帮助!
(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);
tovar list = Enumerable.Range(0, 4);
Your example would work if you added a function overload with a signature that takes a ParallelQuery...
Alternatively, you could use LINQ syntax....
Hope this helps someone!
并行的工作原理是将你的东西放入线程池中。另外,你有多少个核心?如果您正在使用单核机器,则仍然需要大约 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.