#include <iostream>
#include <vector>
#include <list>
#include <numeric>
#include <chrono>
using namespace std;
int main() {
constexpr auto iterations = 10000000;
std::vector<double> double_vector;
std::list<double> double_list;
auto begin_vec = std::chrono::steady_clock::now();
for (auto i = 0; i < iterations; ++i)
double_vector.push_back(i);
auto step_vec = std::chrono::steady_clock::now();
std::accumulate(double_vector.begin(), double_vector.end(), 0.0) / double_vector.size();
auto end_vec = std::chrono::steady_clock::now();
auto begin_list = std::chrono::steady_clock::now();
for (auto i = 0; i < iterations; ++i)
double_list.push_back(i);
auto step_list = std::chrono::steady_clock::now();
std::accumulate(double_list.begin(), double_list.end(), 0.0) / double_vector.size();
auto end_list = std::chrono::steady_clock::now();
std::cout << "Iterations: " << iterations << std::endl;
std::cout << "vector [push,accumulate,total] = ["
<< std::chrono::duration_cast<std::chrono::nanoseconds> (step_vec - begin_vec).count() << ","
<< std::chrono::duration_cast<std::chrono::nanoseconds> (end_vec - step_vec).count() << ","
<< std::chrono::duration_cast<std::chrono::nanoseconds> (end_vec - begin_vec).count()
<< "] ns" << std::endl;
std::cout << "list [push,accumulate,total] = ["
<< std::chrono::duration_cast<std::chrono::nanoseconds> (step_list - begin_list).count() << ","
<< std::chrono::duration_cast<std::chrono::nanoseconds> (end_list - step_list).count() << ","
<< std::chrono::duration_cast<std::chrono::nanoseconds> (end_list - begin_list).count()
<< "] ns" << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bGlzdD4KI2luY2x1ZGUgPG51bWVyaWM+CiNpbmNsdWRlIDxjaHJvbm8+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCgppbnQgbWFpbigpIHsKCWNvbnN0ZXhwciBhdXRvIGl0ZXJhdGlvbnMgPSAxMDAwMDAwMDsKCQoJc3RkOjp2ZWN0b3I8ZG91YmxlPiBkb3VibGVfdmVjdG9yOwoJc3RkOjpsaXN0PGRvdWJsZT4gZG91YmxlX2xpc3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VyYXRpb25fY2FzdDxzdGQ6OmNocm9ubzo6bmFub3NlY29uZHM+IChzdGVwX3ZlYyAtIGJlZ2luX3ZlYykuY291bnQoKSA8PCAiLCIKICAgICAgICAgICAgICA8PCBzdGQ6OmNocm9ubzo6ZHVyYXRpb25fY2FzdDxzdGQ6OmNocm9ubzo6bmFub3NlY29uZHM+IChlbmRfdmVjIC0gc3RlcF92ZWMpLmNvdW50KCkgPDwgIiwiCiAgICAgICAgICAgICAgPDwgc3RkOjpjaHJvbm86OmR1cmF0aW9uX2Nhc3Q8c3RkOjpjaHJvbm86Om5hbm9zZWNvbmRzPiAoZW5kX3ZlYyAtIGJlZ2luX3ZlYykuY291bnQoKQogICAgICAgICAgICAgIDw8ICJdIG5zIiA8PCBzdGQ6OmVuZGw7CiAgICBzdGQ6OmNvdXQgPDwgImxpc3QgW3B1c2gsYWNjdW11bGF0ZSx0b3RhbF0gPSBbIiAKICAgICAgICAgICAgICA8PCBzdGQ6OmNocm9ubzo6ZHVyYXRpb25fY2FzdDxzdGQ6OmNocm9ubzo6bmFub3NlY29uZHM+IChzdGVwX2xpc3QgLSBiZWdpbl9saXN0KS5jb3VudCgpIDw8ICIsIgogICAgICAgICAgICAgIDw8IHN0ZDo6Y2hyb25vOjpkdXJhdGlvbl9jYXN0PHN0ZDo6Y2hyb25vOjpuYW5vc2Vjb25kcz4gKGVuZF9saXN0IC0gc3RlcF9saXN0KS5jb3VudCgpIDw8ICIsIgogICAgICAgICAgICAgIDw8IHN0ZDo6Y2hyb25vOjpkdXJhdGlvbl9jYXN0PHN0ZDo6Y2hyb25vOjpuYW5vc2Vjb25kcz4gKGVuZF9saXN0IC0gYmVnaW5fbGlzdCkuY291bnQoKQogICAgICAgICAgICAgIDw8ICJdIG5zIiA8PCBzdGQ6OmVuZGw7CglyZXR1cm4gMDsKfQ==
Iterations: 10000000
vector [push,accumulate,total] = [84740138,64,84740202] ns
list [push,accumulate,total] = [345217665,29024218,374241883] ns