fork(1) download
  1. #include <algorithm>
  2. #include <array>
  3. #include <chrono>
  4. #include <functional>
  5. #include <iostream>
  6. #include <numeric>
  7. #include <vector>
  8.  
  9. template <size_t N, typename F>
  10. void TestFunc(F lambda)
  11. {
  12. std::array<std::chrono::milliseconds::rep, N> time;
  13.  
  14. for(int i = 0; i < N; ++i)
  15. {
  16. auto t1 = std::chrono::high_resolution_clock::now();
  17. for(int j = 0; j < 1000; ++j)
  18. {
  19. lambda();
  20. }
  21. auto t2 = std::chrono::high_resolution_clock::now();
  22. time[i] = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
  23. }
  24. auto avg = std::accumulate(begin(time), end(time), (std::chrono::milliseconds::rep)0) / time.size();
  25. std::cout << "Average of " << N << " tests is " << avg << "ms.\n";
  26. }
  27.  
  28. int main()
  29. {
  30. std::function<int()> f = []()
  31. {
  32. int sum = 0; for(int i = 0; i < 100000; ++i) sum += i; return sum;
  33. };
  34. TestFunc<10>(f);
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 1.36s 3428KB
stdin
Standard input is empty
stdout
Average of 10 tests is 136ms.