fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using namespace chrono;
  4. using ld = long double;
  5. using ll = long long;
  6.  
  7. template<class time_unit>
  8. class timer: high_resolution_clock {
  9. const time_point start_time;
  10. public:
  11. timer(): start_time(now()) {}
  12. rep elapsed_time() const {
  13. return duration_cast<time_unit>(now()-start_time).count(); } };
  14.  
  15. inline void average_time(ld u) {
  16. cout << "average time per iteration = " << fixed << setprecision(10),
  17. cout << u << " nsec."; }
  18.  
  19. inline ll arithmetic_series_sum(int n) {
  20. timer<nanoseconds> t; ll sum = 0;
  21. for (int i = n; i > 0; --i)
  22. sum += i;
  23. average_time(ld(t.elapsed_time())/n);
  24. return sum; }
  25.  
  26. int main() {
  27. int n; cin >> n, assert(arithmetic_series_sum(n) == 1ll*(n+1)*n/2); }
  28.  
Success #stdin #stdout 0.34s 4464KB
stdin
1000000000
stdout
average time per iteration = 0.3340828980 nsec.