Question

Which operation takes more CPU clocks, modulo or comparison?

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?

Was it helpful?

Solution

If measured in CPU cycles, probably the modulo operation takes more cycles; this may depend on 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 (i.e. affect the instruction pipeline). 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. If you want to optimise your example, you would write something like:

printf ("1234567890");

OTHER TIPS

Comparison is a simple operation and is usually faster (the CPU can use logical operators on bits).

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top