Fastest way to implement Modulo

Revision en1, by Mooncrater, 2020-01-01 17:12:01

See these two submissions:

A

B

The only difference between the two being that A uses a hand-made function I stole from here:

int add(int a, int b) {
 	int res = a + b;
 
 	while (res >= MOD) res -= MOD;
 
 	while (res < 0) res += MOD;
 
 	return res;
}

This function is easy to understand. I am just not sure, why using this only, the running time of the program went down from 936ms to 561ms. That difference is bigger than expected. Can anyone help with, why is this happening? Why exactly is this function faster than simply using %MOD?

Tags modulo

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Mooncrater 2020-01-01 17:12:01 786 Initial revision (published)