fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. #include <sys/time.h>
  6. #include <sys/resource.h>
  7. #include <unistd.h>
  8.  
  9. double get_time()
  10. {
  11. struct timeval t;
  12. struct timezone tzp;
  13. gettimeofday(&t, &tzp);
  14. return t.tv_sec + t.tv_usec*1e-6;
  15. }
  16.  
  17. int main() {
  18. double worst_time = 0.0;
  19. double best_time = 1e6;
  20.  
  21. volatile double x0 = -M_PI/2.0;
  22. volatile double foo = atan(x0); // SLOW CALL HERE
  23. volatile double sum = 0.0; // volatile to avoid having tan() call optimized away
  24. for (double x = x0; x < M_PI/3.0; x += 0.1) {
  25. volatile double y = x;
  26. const double start = get_time();
  27. const double value = atan(y);
  28. const double end = get_time();
  29. sum += value;
  30.  
  31. const double delta = end - start;
  32. if (delta > worst_time) {
  33. worst_time = delta;
  34. }
  35. if (delta < best_time) {
  36. best_time = delta;
  37. }
  38. printf("* %f (value: %f)\n", delta, y);
  39. }
  40.  
  41. printf("%f / %f\n", worst_time, best_time);
  42.  
  43. printf("%f\n", foo);
  44. }
  45.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
* 0.000000 (value: -1.570796)
* 0.000001 (value: -1.470796)
* 0.000001 (value: -1.370796)
* 0.000000 (value: -1.270796)
* -0.000000 (value: -1.170796)
* 0.000001 (value: -1.070796)
* 0.000001 (value: -0.970796)
* 0.000001 (value: -0.870796)
* 0.000001 (value: -0.770796)
* 0.000001 (value: -0.670796)
* 0.000000 (value: -0.570796)
* -0.000000 (value: -0.470796)
* 0.000001 (value: -0.370796)
* 0.000001 (value: -0.270796)
* 0.000005 (value: -0.170796)
* 0.000000 (value: -0.070796)
* -0.000000 (value: 0.029204)
* -0.000000 (value: 0.129204)
* 0.000001 (value: 0.229204)
* 0.000001 (value: 0.329204)
* -0.000000 (value: 0.429204)
* 0.000000 (value: 0.529204)
* 0.000001 (value: 0.629204)
* -0.000000 (value: 0.729204)
* 0.000000 (value: 0.829204)
* 0.000001 (value: 0.929204)
* -0.000000 (value: 1.029204)
0.000005 / -0.000000
-1.003885