fork download
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <gmp.h>
  5.  
  6. long long int div_mul(long long int x) {
  7. return (x / 10) * 10;
  8. }
  9.  
  10. long long int sub_mod(long long int x) {
  11. return (x - x % 10);
  12. }
  13.  
  14. int main() {
  15.  
  16. long long int testVal = INT64_MAX;
  17. printf("%lld\n", div_mul(testVal));
  18. printf("%lld\n", sub_mod(testVal));
  19.  
  20. const int limit = 1000000000;
  21.  
  22. const double checkPoint0 = (double) clock() / CLOCKS_PER_SEC;
  23.  
  24. for (unsigned volatile i = 0; i < limit; ++i) {
  25. long long int x = testVal;
  26. long long int y = div_mul(x);
  27. }
  28.  
  29. const double checkPoint1 = (double) clock() / CLOCKS_PER_SEC;
  30. const double time_div_mul = checkPoint1 - checkPoint0;
  31.  
  32. printf("time with division followed by multiplication: %f\n", time_div_mul);
  33.  
  34. const double checkPoint2 = (double) clock() / CLOCKS_PER_SEC;
  35.  
  36. for (unsigned volatile i = 0; i < limit; ++i) {
  37. long long int x = testVal;
  38. long long int y = sub_mod(x);
  39. }
  40.  
  41. const double checkPoint3 = (double) clock() / CLOCKS_PER_SEC;
  42. const double time_sub_mod = checkPoint3 - checkPoint2;
  43.  
  44. printf("time with subtraction and modulo: %f\n", time_sub_mod);
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 3.12s 5576KB
stdin
Standard input is empty
stdout
9223372036854775800
9223372036854775800
time with division followed by multiplication: 1.551297
time with subtraction and modulo: 1.562676