简体   繁体   English

哪个操作需要更多CPU时钟,模数或比较?

[英]which operation takes more CPU clocks, modulo or comparison?

Which operation takes more CPU clocks, modulo or comparison ? 哪个操作需要更多CPU时钟, modulocomparison

Will this code take more time: 这段代码会花费更多时间:

for(j = i; j <= 10; j++)
{
   if(j == 10) printf("0");
   else printf("%d", j);
}

or this 或这个

for(j = i; j <= 10; j++)     
   printf("%d", j % 10);

and why? 为什么?

If measured in CPU cycles, probably the modulo operation takes more cycles; 如果以CPU周期测量,模数运算可能需要更多周期; this may depend on CPU. 这可能取决于CPU。 However, CPU cycles aren't a great way to measure performance with modern processors which run more than one instruction at once (pipelining), have multiple layers of cache etc. In this case, putting an additional test in will mean an additional branch, which may be more significant in terms of timing (ie affect the instruction pipeline). 但是,CPU周期不是衡量性能的好方法,现代处理器一次运行多个指令(流水线),有多层缓存等。在这种情况下,进行额外测试将意味着额外的分支,这在时序方面可能更重要(即影响指令流水线)。 The only way to know for sure is to compile it optimised, and time it. 确切知道的唯一方法是对其进行优化编译,并将其计时。

I know your example is meant to be just that, an example, but this also illustrates premature optimisation . 我知道你的例子只是一个例子,但这也说明了过早的优化 The call to printf will take orders of magnitude more time than the modulo or compare. printf的调用将比模数或比较花费更多的时间。 If you want to optimise your example, you would write something like: 如果你想优化你的例子,你会写如下:

printf ("1234567890");

Comparison is a simple operation and is usually faster (the CPU can use logical operators on bits). 比较是一个简单的操作,通常更快(CPU可以在位上使用逻辑运算符)。

If you perform a modulo to a number that is not a power of two, the CPU has to perform a division, that can be a quite expensive operation (of course it depends on the size of the numbers you are using). 如果对不是2的幂的数字执行模数,则CPU必须执行除法,这可能是非常昂贵的操作(当然,这取决于您使用的数字的大小)。

Speaking of cpu clocks, a comparison can be done in parallel, since you can just use a xor operation, so doing x==10 or x==200000 will take the same small amount of cpu clocks. 说到cpu时钟,可以并行进行比较,因为你可以只使用xor操作,所以做x==10x==200000会占用相同的少量cpu时钟。 With a division this is not possible and a bigger number will require more time. 通过划分这是不可能的,更大的数字将需要更多的时间。

In terms of Assembly, a modulo operation implies a "never so easy" multiplication. 在汇编方面,模运算意味着“从未如此简单”的乘法。 See some algorithms . 看一些算法 A branch operation actually is the second fastest instruction (jump is the first) as it only takes at most one substraction to do the comparison. 分支操作实际上是第二快的指令(跳跃是第一个),因为它只需要最多一个减法来进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM