Created
May 16, 2022 01:28
-
-
Save yknishidate/7700d2c3df4b27b67da54008bf5e6a1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
}); | |
} | |
} |
Author
yknishidate
commented
May 16, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment