Skip to content

Instantly share code, notes, and snippets.

@yknishidate
Created May 16, 2022 01:28
Show Gist options
  • Save yknishidate/7700d2c3df4b27b67da54008bf5e6a1d to your computer and use it in GitHub Desktop.
Save yknishidate/7700d2c3df4b27b67da54008bf5e6a1d to your computer and use it in GitHub Desktop.
#include <chrono>
#include <vector>
#include <random>
#include <optional>
#include <iostream>
#include <execution>
#include <functional>
void calcTime(const std::function<void(void)>& func)
{
auto start = std::chrono::system_clock::now();
func();
auto end = std::chrono::system_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << std::endl;
}
int main()
{
std::mt19937 engine;
std::uniform_real_distribution<double> distribution(0.0, 1.0);
for(int num : {100, 10000, 1000000}){
std::cout << "num: " << num << std::endl;
std::vector<double> vec(num);
calcTime([&](){
std::for_each(std::execution::par, vec.begin(), vec.end(), [&](double& value) { value = distribution(engine); });
});
calcTime([&](){
for(int i = 0; i < vec.size(); ++i){
vec[i] = distribution(engine);
}
});
}
}
@yknishidate
Copy link
Author

num: 100
88
2

num: 10000
281
373

num: 1000000
14138
27528

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment