fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <numeric>
  5. #include <chrono>
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. int main() {
  12. constexpr auto iterations = 10000000;
  13.  
  14. std::vector<double> double_vector;
  15. std::list<double> double_list;
  16.  
  17. auto begin_vec = std::chrono::steady_clock::now();
  18. for (auto i = 0; i < iterations; ++i)
  19. double_vector.push_back(i);
  20. auto step_vec = std::chrono::steady_clock::now();
  21. std::accumulate(double_vector.begin(), double_vector.end(), 0.0) / double_vector.size();
  22. auto end_vec = std::chrono::steady_clock::now();
  23.  
  24. auto begin_list = std::chrono::steady_clock::now();
  25. for (auto i = 0; i < iterations; ++i)
  26. double_list.push_back(i);
  27. auto step_list = std::chrono::steady_clock::now();
  28. std::accumulate(double_list.begin(), double_list.end(), 0.0) / double_vector.size();
  29. auto end_list = std::chrono::steady_clock::now();
  30.  
  31. std::cout << "Iterations: " << iterations << std::endl;
  32. std::cout << "vector [push,accumulate,total] = ["
  33. << std::chrono::duration_cast<std::chrono::nanoseconds> (step_vec - begin_vec).count() << ","
  34. << std::chrono::duration_cast<std::chrono::nanoseconds> (end_vec - step_vec).count() << ","
  35. << std::chrono::duration_cast<std::chrono::nanoseconds> (end_vec - begin_vec).count()
  36. << "] ns" << std::endl;
  37. std::cout << "list [push,accumulate,total] = ["
  38. << std::chrono::duration_cast<std::chrono::nanoseconds> (step_list - begin_list).count() << ","
  39. << std::chrono::duration_cast<std::chrono::nanoseconds> (end_list - step_list).count() << ","
  40. << std::chrono::duration_cast<std::chrono::nanoseconds> (end_list - begin_list).count()
  41. << "] ns" << std::endl;
  42. return 0;
  43. }
Success #stdin #stdout 0.57s 393964KB
stdin
Standard input is empty
stdout
Iterations: 10000000
vector [push,accumulate,total] = [84740138,64,84740202] ns
list [push,accumulate,total] = [345217665,29024218,374241883] ns