通过 c# 使用 pc 中的所有核心

发布于 2024-12-26 02:37:32 字数 2103 浏览 3 评论 0原文

*请注意,我只是在测试以理解这一点。

我试图通过 Parallel.For() 方法使用计算机的所有核心。这工作得很好,但是当我用普通的 for 循环尝试相同的方法时,速度要快得多。并行方法需要 16 秒,而普通方法只需 6 秒。

我希望你能告诉我我在这里做错了什么。

更新的代码

        DateTime parallelStart = new DateTime();
        DateTime parallelFinish = new DateTime();
        DateTime singeStart = new DateTime();
        DateTime singeFinish = new DateTime();
        parallelStart = DateTime.Now;
        int inputData = 0;

        Parallel.For(0, 1000000000, i =>
        {
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
        });

        parallelFinish = DateTime.Now;
        singeStart = DateTime.Now;

        for (int i = 0; i < 1000000000; i++)
        {
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
        }

        singeFinish = DateTime.Now;
        MessageBox.Show("Parallel execution time: " + (parallelFinish - parallelStart).Seconds + "\n" +
                        "Singe execution time: " + (singeFinish - singeStart).Seconds);

第一个代码:

DateTime parallelStart = new DateTime();
DateTime parallelFinish = new DateTime();
DateTime singeStart = new DateTime();
DateTime singeFinish = new DateTime();
parallelStart = DateTime.Now;

Parallel.For(0, 2000000000, i =>
{
    var inputData = 0;
});

parallelFinish = DateTime.Now;
singeStart = DateTime.Now;

for (int i = 0; i < 2000000000; i++)
{
    var inputData = 0;
}

singeFinish = DateTime.Now;
MessageBox.Show("Parallel execution time: " + (parallelFinish - parallelStart).Seconds + "\n" + "Singe execution time: " + (singeFinish - singeStart).Seconds);

*Please note that I am just testing to understand this.

I am trying to use all the cores of my computer with the Parallel.For() method. This is working just fine, but when I am trying the same method with a normal for loop it is going much faster. The parallel method is taking 16 sec and the normal method is only taking 6 sec.

I hope you can tell me what I am doing wrong here.

Updated code

        DateTime parallelStart = new DateTime();
        DateTime parallelFinish = new DateTime();
        DateTime singeStart = new DateTime();
        DateTime singeFinish = new DateTime();
        parallelStart = DateTime.Now;
        int inputData = 0;

        Parallel.For(0, 1000000000, i =>
        {
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
        });

        parallelFinish = DateTime.Now;
        singeStart = DateTime.Now;

        for (int i = 0; i < 1000000000; i++)
        {
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
            inputData = inputData++;
        }

        singeFinish = DateTime.Now;
        MessageBox.Show("Parallel execution time: " + (parallelFinish - parallelStart).Seconds + "\n" +
                        "Singe execution time: " + (singeFinish - singeStart).Seconds);

First code:

DateTime parallelStart = new DateTime();
DateTime parallelFinish = new DateTime();
DateTime singeStart = new DateTime();
DateTime singeFinish = new DateTime();
parallelStart = DateTime.Now;

Parallel.For(0, 2000000000, i =>
{
    var inputData = 0;
});

parallelFinish = DateTime.Now;
singeStart = DateTime.Now;

for (int i = 0; i < 2000000000; i++)
{
    var inputData = 0;
}

singeFinish = DateTime.Now;
MessageBox.Show("Parallel execution time: " + (parallelFinish - parallelStart).Seconds + "\n" + "Singe execution time: " + (singeFinish - singeStart).Seconds);

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

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

发布评论

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

评论(8

奈何桥上唱咆哮 2025-01-02 02:37:33

代码的并行化涉及相当大的开销,并且您使用它来执行琐碎的操作。这就是您的代码运行速度非常慢的原因。只有充实代码后,您才会看到改进,并且您的代码执行大量 CPU 密集型操作。

Parallelization of code involves a fairly large overhead, and you are using it to perform trivial operations. That's why your code runs very slowly. You will only see an improvement once you flesh-in your code, and your code does plenty of CPU-intensive operations.

椵侞 2025-01-02 02:37:33

正如大多数有关并行编程的书籍都会提到的那样,“始终衡量性能”。不要假设并行比使用直接循环更快,因为并行性包含开销。

您确实需要考虑需要处理多少数据、并发级别以及需要发生多少易失性数据共享(需要多少锁定)。

As most books on parallel programming will mention, "always measure performance." Don't assume that going parallel is faster than using a straight forward loop, because parallelism includes overhead.

You really need to consider how much data needs to be processed, the level of concurrency and and how much sharing of volatile data needs to occur (how much locking is required).

月隐月明月朦胧 2025-01-02 02:37:33

测量时间时使用 TotalSeconds 而不是 Seconds。

Use TotalSeconds instead of Seconds when measuring time.

从此见与不见 2025-01-02 02:37:33

在没有我自己的经验证据的情况下,我怀疑对额外线程进行排队的开销超过了好处。您要求的是同时执行 20 亿个工作,如果不检查,我怀疑使用线程池进行并行队列。

I suspect, without empirical evidence of my own, that the overhead of queuing the additional thread outweights the benefit. What you are asking for is 2bn pieces of work to be executed concurrently, which without checking I suspect Parallel queues with the ThreadPool.

翻了热茶 2025-01-02 02:37:32

那么,哪个更快:自己不做任何工作,还是雇佣十个人,每人做十分之一的不工作?

你做错的是你的例子完全是人为的。抖动完全意识到,将零分配给一遍又一遍未使用的同一个本地是没有意义的,因此它可能会消除这项工作。即使不是,这项工作也是微不足道的。设置所有线程的工作比您自己完成的工作要大。

做一个真正的测试——做一些光线追踪或生成分形或其他东西。 做一些现实的事情,在一个线程上需要几分钟或几小时。您不能合理地期望在一项太简单的工作上看到节省。

Well, what's faster: doing no work yourself, or hiring ten guys to each do one tenth of no work?

What you're doing wrong is that your example is completely artificial. The jitter is fully aware that assigning zero to the same local that is unused over and over again is pointless, so it is probably eliminating the work. Even if it is not, that work is trivial. The work of setting up all the threads is larger than just doing the work yourself.

Do a real test -- do some raytracing or generate a fractal or something in there. Do something realistic that will take minutes or hours on one thread. You cannot reasonably expect to see a savings on a job that is too easy.

青春有你 2025-01-02 02:37:32

这可能是因为创建线程的开销以及线程之间的争用比操作本身花费更多的时间。尝试在该循环中放入更复杂的东西,我敢打赌你的结果会改变。

This is probably because the overhead of creating the threads and the contention between them takes more time than the operation itself. Try putting something more complex inside that loop and I'd bet your results will change.

中二柚 2025-01-02 02:37:32

设置堆栈上现有值类型的值非常简单。与此相比,创建和跟踪线程的成本会很大。 为您的操作添加任何复杂性都会极大地改变您的结果。即使创建和设置一个字符串变量等于附加两个字符串也会大大增加所做的工作。尝试一些更复杂的东西(几乎任何东西都可以!),您会看到并行库的价值增加。

试试这个:

Parallel.For(0, 2000000000, i =>
{
    var string inputData = "ninjas " + "like " + "taco";
});

不管你信不信,我们在这里添加了更多处理。 (正在创建多个字符串并在后台抛出)这将改变您的结果。然后,当您考虑到我们在这些循环中执行更加复杂的事情时,例如:

  • 数据访问
  • 磁盘 IO、
  • 复杂的数学
  • 过滤/排序/列表投影等,

结果将非常有利于并行性。

Setting the value of an existing value type on the stack is incredibly, profoundly trivial. The cost of creating and keeping track of the threads would be large compared to this. Adding any complexity to your operation would drastically change your results. Even creating and setting a string variable equal to the appending of two strings would massively increase the work done. Try something more complicated (almost anything will do!) and you'll see the value of the parallel library increase.

Try this:

Parallel.For(0, 2000000000, i =>
{
    var string inputData = "ninjas " + "like " + "taco";
});

Believe it or not, we have added a lot more processing here. (There's multiple strings being created and tossed in the background) This will change your results. And then when you consider that we do vastly more complicated things in these loops, like:

  • data-access
  • disk IO
  • complicated math
  • filtering / ordering / projecting of lists, etc.

, the results will be dramatic in favor of parallelism.

护你周全 2025-01-02 02:37:32

多线程有开销。对于与设置线程所花费的时间相比需要较长时间的操作来说,这很好。

但就您而言,您有一个非常简单的循环体,并且创建线程来完成工作的开销超过了并行执行的好处。

Multithreading has overhead. For operations that take a long time compared to the time taken to set up the thread, great.

But in your case, you have a very simple loop body, and the overhead of creating the threads to do the work is outweighing the benefit of doing it in parallel.

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