fork download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <time.h>
  4. #include <cmath>
  5.  
  6.  
  7. using namespace std;
  8. using namespace std::chrono;
  9.  
  10. template<typename lambda>
  11. double timed_template(lambda &&fnc) {
  12. high_resolution_clock::time_point t1 = high_resolution_clock::now();
  13. //auto start = chrono::steady_clock::now();
  14. fnc(10, 1000000000, 7);
  15. high_resolution_clock::time_point t2 = high_resolution_clock::now();
  16.  
  17. auto duration = duration_cast<nanoseconds>(t2 - t1).count();
  18. //auto end = chrono::steady_clock::now();
  19. //auto duration = chrono::duration <double, nano>(end - start).count();
  20. return duration;
  21. }
  22.  
  23. int main()
  24. {
  25.  
  26. printf("modulo fct: %f ns\n",
  27. timed_template([](int X, int Y, int D) ->int {
  28. if ((Y - X) % D == 0)
  29. return (Y - X) / D;
  30. else
  31. return ((Y - X) / D) + 1;
  32. })
  33. );
  34.  
  35. printf("ceil and double: %f ns\n",
  36. timed_template([](int X, int Y, int D) ->int {
  37. double diff = Y - X;
  38. long hop = (long)ceil(diff / D);
  39. return hop;
  40. })
  41. );
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
modulo fct:      315.000000 ns
ceil and double: 237.000000 ns