fork download
  1. #include <algorithm>
  2. #include <string>
  3.  
  4. struct sort_algo {
  5. template <typename It>
  6. void operator()(It f, It l) const { std::sort(f,l); }
  7. };
  8.  
  9. struct makeheap_algo {
  10. template <typename It>
  11. void operator()(It f, It l) const { std::make_heap(f,l); }
  12. };
  13.  
  14. struct reverse_algo {
  15. template <typename It>
  16. void operator()(It f, It l) const { std::reverse(f,l); }
  17. };
  18.  
  19. template <typename It, typename Algo>
  20. void benchmark(It begin, It end, Algo algorithm)
  21. {
  22. for (unsigned long i=0; i < (1ul<<10); ++i)
  23. algorithm(begin, end); // TODO add timing
  24. }
  25.  
  26. int main()
  27. {
  28. std::string hello = "hello world";
  29. int data[] = { 1, 3, 7, -9, 0 };
  30.  
  31. benchmark(hello.begin(), hello.end(), sort_algo());
  32. benchmark(hello.begin(), hello.end(), makeheap_algo());
  33. benchmark(hello.begin(), hello.end(), reverse_algo());
  34.  
  35. benchmark(data, data+5, sort_algo());
  36. benchmark(data, data+5, makeheap_algo());
  37. benchmark(data, data+5, reverse_algo());
  38. }
  39.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
Standard output is empty