为什么多个核心参与顺序算法?

发布于 2024-12-21 23:06:15 字数 631 浏览 7 评论 0原文

当我在 C# 中运行冒泡排序、鸡尾酒排序和快速排序时,我可以看到所有 3 个内核都在我的 AMD X3 上运行(X4 附带 1 个损坏的内核)。

为什么会发生这种情况?我的算法是顺序的,我的代码没有任何线程标签。特别是像排序算法这样的算法,它是一种高度顺序的算法,不,一个事件在下一个事件完成之前不会发生。它是如何分解算法的?

例如,根据要求进行冒泡排序:

 public void BubbleSort()
    {
        for (int i = 1; i < amount; i++)
        {

            for (int j = 0; j < a; j++)
            {
                if (numbers[j] > numbers[j + 1])
                {
                    t = numbers[j + 1];
                    numbers[j + 1] = numbers[j];
                    numbers[j] = t;
                }
             }
          a--;
        }

    }

When I run bubblesorts, cocktailsorts and quicksorts in C#, I can see that all 3 cores are engaged on my AMD X3 (X4 shipped with 1 broken core).

Why is this happening? My algorithm is sequential and my code doesn't have any thread tags. Especially something like sorting algorithms which is a highly sequential algorithm no, one event can't happen until the next is completed. How does it manage to split up the algorithm?

The bubblesort for example on request:

 public void BubbleSort()
    {
        for (int i = 1; i < amount; i++)
        {

            for (int j = 0; j < a; j++)
            {
                if (numbers[j] > numbers[j + 1])
                {
                    t = numbers[j + 1];
                    numbers[j + 1] = numbers[j];
                    numbers[j] = t;
                }
             }
          a--;
        }

    }

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

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

发布评论

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

评论(2

很酷又爱笑 2024-12-28 23:06:15

您的代码可能会在上下文切换时交换核心。但一次只会使用一个。

Your code can potentially swap cores on a context switch. But will only use one at a time.

喜爱皱眉﹌ 2024-12-28 23:06:15

排序算法可以并行运行并利用多个内核。您使用哪种排序例程?它们很可能不是顺序算法。

例如,快速排序非常容易通过分而治之的方式实现并行化

Sorting algorithms can be made to run in parallel and utilize multiple cores. Which sort routines are you using? It's very possible that they are not sequential algorithms.

As an example, Quicksort is very easy to parallelize via divide and conquer.

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