fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <chrono>
  5.  
  6. using namespace std;
  7.  
  8. #define MAXN 100000000
  9.  
  10. class timer
  11. {
  12. using T = std::chrono::time_point<std::chrono::steady_clock>;
  13. T t1, t2;
  14. public:
  15. void start() { t1 = std::chrono::steady_clock::now(); }
  16. void stop() { t2 = std::chrono::steady_clock::now(); }
  17. void print(const char *title) const { std::cout << title << (t2 - t1).count() << endl; }
  18. };
  19.  
  20. int main()
  21. {
  22. vector <unsigned> v(MAXN);
  23. unsigned s;
  24. timer t;
  25.  
  26. srand(time(0));
  27.  
  28. s = 0;
  29. for (unsigned q=0; q<MAXN; ++q) v[q] = rand();
  30. t.start();
  31. for (unsigned q=0, n=v.size(); q<n; ++q) s += v[q];
  32. t.stop();
  33. cout << s << endl;
  34. t.print("Index: ");
  35.  
  36. s = 0;
  37. for (unsigned q=0; q<MAXN; ++q) v[q] = rand();
  38. t.start();
  39. for (auto x : v) s += x;
  40. t.stop();
  41. cout << s << endl;
  42. t.print("Byval: ");
  43.  
  44. s = 0;
  45. for (unsigned q=0; q<MAXN; ++q) v[q] = rand();
  46. t.start();
  47. for (auto &x : v) s += x;
  48. t.stop();
  49. cout << s << endl;
  50. t.print("Byref: ");
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 2.4s 15240KB
stdin
Standard input is empty
stdout
3852871726
Index: 41888244
1275459847
Byval: 42361756
2080980504
Byref: 43436973