在LINQ中并行加载图像 [英] Parallel loading images in LINQ

查看:115
本文介绍了在LINQ中并行加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试并行和LINQ.看下面的代码.它可以工作,但是只是为了得到这个主意:

I am experimenting with parallel and LINQ. Look at the code below. It works, but just to get the idea:

private void LoadImages(string path)
{
    images =
        Directory.GetFiles(path)
        .Select(f => GetImage(f))
        .ToList();
}

private Image GetImage(string path)
{
    return Image.FromFile(path);
}

所以我基本上是从指定目录中找到的每个文件中获取图像的.问题是-如何使之平行?现在,这就像遍历它们.我想以某种方式并行化它.不知何故,因为我太缺乏经验,无法提出实现该想法的想法,所以这就是为什么我要求你们,指望一些帮助使之更快的原因:)

So I am basically getting an image from each file found in the specified directory. The question is - how to make this parallel? Right now it is like iterating over them. I would like to parallelize it "somehow". Somehow, because I am too inexperienced to suggest an idea how to achieve this, so that's why I am asking you, guys, counting on some help to make this faster :)

推荐答案

使用PLINQ:

var images=(from file in Directory.EnumerateFiles(path).AsParallel()
           select GetImage(file)).ToList();

读取图像不受CPU限制,因此您可以指定更高的并行度:

Reading the images isn't CPU bound, so you can specify a higher degree of parallelism:

var images=(from file in Directory.EnumerateFiles(path)
                                  .AsParallel()
                                  .WithDegreeOfParallelism(16)
           select GetImage(file)).ToList();

这篇关于在LINQ中并行加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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