MOD 运算是否比乘法更消耗 CPU 资源?

发布于 2024-10-01 13:09:07 字数 544 浏览 1 评论 0原文

为什么 mod (%) 运算比乘法 (*) 运算的成本略高于 2 倍数?

请更具体地说明CPU如何进行除法运算并返回MOD运算的结果。

在以下示例中,每个线程运行一秒钟。该测试是在 SPARC 处理器上执行的。

// multiplication
void someThread() {

    int a = 10234;
    while (true) {
        opers++;
        a = a * a;
        a++;
    }

    // opers ~ 26 * 10^6 in a sec.
}

// MOD
void someThread() {

    int a = 10234;
    while (true) {
        opers++;
        a = a % 10000007;
        a++;
    }

    // opers ~ 12 * 10^6 in a sec.
}

Why is a mod (%) operation more expensive than a multiplication (*) by a bit more than a factor of 2?

Please be more specific about how CPU performs division operation and returns the result for MOD operation.

In the following example the threads each run for a second. The test was performed on a SPARC processor.

// multiplication
void someThread() {

    int a = 10234;
    while (true) {
        opers++;
        a = a * a;
        a++;
    }

    // opers ~ 26 * 10^6 in a sec.
}

// MOD
void someThread() {

    int a = 10234;
    while (true) {
        opers++;
        a = a % 10000007;
        a++;
    }

    // opers ~ 12 * 10^6 in a sec.
}

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

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

发布评论

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

评论(5

廻憶裏菂餘溫 2024-10-08 13:09:07

MOD 是除法运算,而不是乘法运算。除法比乘法更昂贵。

有关 MOD 操作的更多信息,请访问:http://en.wikipedia.org/wiki/Modulo_operation

MOD is a division operation, not a multiplication operation. Division is more expensive than multiplication.

More information about the MOD operation here: http://en.wikipedia.org/wiki/Modulo_operation

天暗了我发光 2024-10-08 13:09:07

AMD 和 Intel x86 处理器的指令延迟和吞吐量

一项操作本身就较慢中央处理器 :)

Instruction latencies and throughput for AMD and Intel x86 processors

One operation is just inherently slower at the CPU :)

流绪微梦 2024-10-08 13:09:07

用于除法的算法(处理器通过在门中实现的算法执行除法和乘法)比乘法成本更高。事实上,一些复杂度较高的除法算法是使用乘法作为基本步骤。

即使您使用在学校学到的简单算法。它们都具有相同的渐近复杂度,但除法的常数更大(您必须找出数字,这不是微不足道的,因此您可能会搞砸并必须修复混乱)。

Algorithms (processors execute the division and the multiplication by algorithms implemented in gates) for division are more costly than for multiplication. As a matter of fact, some algorithms for division which have a good complexity are using the multiplication as a basic step.

Even if you use the naive algorithms that are learned in school. They both have the same asymptotic complexity, but the constant for the division is greater (you have to find out the digit and that is not trivial, so you can mess up and have to fix the mess).

奶气 2024-10-08 13:09:07

mod 本质上与除法相同的过程(出于这个原因,某些系统提供了“divmod”)。

二进制长乘法和二进制长除法之间的最大区别在于,长除法要求您在每次减法后执行溢出测试,而长乘法在初始屏蔽过程后无条件执行加法。

这意味着您可以轻松地重新排列和并行化长乘法中的加法,但不能对长除法执行相同的操作。我在 https://stackoverflow.com/a/53346554/5083516 写了一个更长的答案

mod is essentially the same process as division (some systems provide a "divmod" for this reason).

The big difference between binary long mulitplication and binary long division is that long division requires you to perform an overflow test after each subtraction, while long mutiplication performs the addition unconditionally after the initial masking process.

That means you can easilly rearrange and paralleise the addditions in long multiplication, but you can't do the same for long division. I wrote a longer answer about this at https://stackoverflow.com/a/53346554/5083516

沙沙粒小 2024-10-08 13:09:07

是的,mod 比乘法更昂贵,因为它是通过除法实现的。 (CPU 通常在除法时返回商和余数。)但是两个线程都使用乘法。复制/粘贴错误?

Yes, mod is more expensive than multiplication, as it is implemented through division. (CPUs generally return both quotient and remainder on division.) But both of your threads use multiplication. copy/paste error?

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