Module Calculation Execution Time (%)

1
  

Variable declaration:

var x= 200;

var y= 50;
  

Module:

var resposta = x % y;
  

What the module should do, I believe (calculate):

s1 = x / y;
s2 = s1 * y;
resposta = s2 - x;

The question itself is: Does the calculation that the module does, takes the same execution time as if I did it in the hand?

    
asked by anonymous 02.07.2014 / 16:09

1 answer

4

As this answer in SOen , you can calculate the and quotient the rest of a split with a single IDIV operation. That is, each of these 3 code snippets:

// Trecho 1
var quociente = x / y;

// Trecho 2
var resto = x % y;

// Trecho 3
var quociente = x / y;
var resto = x % y;

could at first be executed with only 1 instruction (that is, the two instructions in high-level code in section 3 would be translated into a single low-level instruction).

I say could , because I do not know how it is done in language-to-language practice: it is expected that compilers will be able to do this conversion correctly (perhaps except for part 3, where the difficulty in optimization is greater). But interpreted languages, languages with dynamic data types, etc. can implement this in another way. The only way to know for sure would be to parse the generated low level code (as the linked response did, for C ++).

P.S. I know that "module" ( mod ) is different from "rest" ( rem ) - there are circumstances where they are different, in particular involving negative numbers - but for purposes of that answer I considered them equivalent. >     

02.07.2014 / 16:30