Is MOD operation more CPU intensive than multiplication?

隐身守侯 提交于 2019-11-30 01:16:56

问题


Why is MOD operation more expensive than 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.
}

回答1:


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).




回答2:


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




回答3:


Instruction latencies and throughput for AMD and Intel x86 processors

One operation is just inherently slower at the CPU :)




回答4:


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?




回答5:


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



来源:https://stackoverflow.com/questions/4109330/is-mod-operation-more-cpu-intensive-than-multiplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!