C# 中的 Parallel.Foreach 和并行执行

发布于 2024-10-17 19:02:51 字数 578 浏览 3 评论 0原文

我的任务是并行扫描所有名称以“xyz”开头的文件夹。我的意思是,如果一个文件夹同时被扫描,其他文件夹也应该被扫描。我不想一一扫描。

为此,我使用了 Parallel Foreach。

问题是? 正确与否?以及如何知道它是否并行运行(将任何消息放在某个位置)?

Parallel.ForEach(path, currentPath =>
   {
    var output = programReader.GetData(currentPath, durReader.dirPattern);

    foreach (var item in output)
      {
        foreach (var project in item.Name)
           Console.WriteLine(item.serverName + " " + item.serverNumber + " " + fileName);
        }
     }

编辑:

是Parallel.Foreach仅适用于多核系统,或者它也可以在单核系统上工作以执行显示并行性

I am having a task of scanning all the folders name start with "xyz" parallely. I meant if one folder getting scan same time other one should also getting scan. I don't want one by one scanning.

For that I used Parallel Foreach.

Question is?
Is it correct or not? and How to know is it running parallely(to put any message some where)?

Parallel.ForEach(path, currentPath =>
   {
    var output = programReader.GetData(currentPath, durReader.dirPattern);

    foreach (var item in output)
      {
        foreach (var project in item.Name)
           Console.WriteLine(item.serverName + " " + item.serverNumber + " " + fileName);
        }
     }

EDIT:

Is Parallel.Foreach only works on multicore systems or it could work on single core system also to perform show parallelism

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

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

发布评论

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

评论(4

谁许谁一生繁华 2024-10-24 19:02:51

首先——如果你问一个问题,就让它成为一个问题。 !!是一个很好的指示,这不是一个问题。

其次,你的方法毫无意义。并行将会非常并行。好的。坏:你仍然只有一张光盘。结果:任务等待。在硬件支持的程度以上并行化 IO 操作是没有意义的。

Foirst - if you ask a question, make it a question. !! is a good indication it is not a question.

Second, your approach makes little sense. Parallel will go VERY parallel. Good. Bad: you still have only one disc. Result: tasks waiting. It makes no sense to paralellize IO operations over the degree supported by the hardware.

拥抱影子 2024-10-24 19:02:51

并行扩展将负载分配给每个核心 - 尽管我不确定它们是否考虑了超线程核心。

但是,添加另一个答案来引导您走向正确的方向:

不要尝试并行执行此操作。磁盘一次只能处理一个请求,因此您可能会减慢所有操作,因为磁盘队列可能会变得更大。

您可能能够获得更高性能的唯一情况是您正在扫描的位置实际上是存储分布且高度复制的 SAN。

The Parallel extensions split the load per core - although I'm not sure if they take into account hyperthreaded cores.

But, to add another answer to steer you in the right direction:

Don't try and do this in parallel. The disk can only serve one request at a time, so you're likely to just slow everything down as the disk queue is just likely to get bigger.

The only scenario where you might be able to get increased performance is if the location you're scanning is actually a SAN where the storage is distributed and highly replicated.

难理解 2024-10-24 19:02:51

您可以打印 Thread.ManagedThreadId 值来查看正在使用哪些线程。

You can print the Thread.ManagedThreadId value to see which threads are being used.

烛影斜 2024-10-24 19:02:51

Parallel.ForEach 或 Parallel.Execute 使用处理器的核心。因此,如果您的处理器有多个核心,它们将被同等地用于运行该线程。你在这里

Parallel.ForEach or Parallel.Execute uses the cores of the processor. So if your processor is having more thn one core they will be used equally to run this thread. Here you are m

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