cdivisionmodulocpu-cycles

which operation takes more CPU clocks, modulo or comparison?


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?


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");