Last active
October 8, 2022 10:41
-
-
Save lucidfrontier45/c83a58ff423f26dc0feb9ce84ea8f836 to your computer and use it in GitHub Desktop.
cpp benchmark
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 <iostream> | |
#include <random> | |
double dot(const std::vector<double> &x, const std::vector<double> &y) | |
{ | |
auto n = x.size(); | |
double s = 0.0; | |
for (size_t i = 0; i < n; i++) | |
{ | |
s += x[i] * y[i]; | |
} | |
return s; | |
} | |
std::uniform_real_distribution<> dist(0.0, 1.0); | |
template <class URBG> | |
std::vector<double> uniform_rand(size_t n, URBG &engine) | |
{ | |
std::vector<double> v(n); | |
for (size_t i = 0; i < n; i++) | |
{ | |
v[i] = dist(engine); | |
} | |
return v; | |
} | |
int main() | |
{ | |
std::random_device seed_gen; | |
std::default_random_engine engine(0); | |
double s = 0.0; | |
auto start = std::chrono::system_clock::now(); | |
for (size_t i = 0; i < 10000; i++) | |
{ | |
auto x = uniform_rand(10000, engine); | |
auto y = uniform_rand(10000, engine); | |
s += dot(x, y); | |
} | |
auto end = std::chrono::system_clock::now(); | |
double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.0; | |
std::cout << "s = " << s << std::endl; | |
std::cout << "elapsed = " << elapsed << "[sec]" << std::endl; | |
} |
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 <iostream> | |
#include <vector> | |
int main() | |
{ | |
double s = 0.0; | |
auto start = std::chrono::system_clock::now(); | |
for (size_t i = 0; i < 5000; i++) | |
{ | |
std::vector<double> v(1000000); | |
v[0] = 10; | |
} | |
auto end = std::chrono::system_clock::now(); | |
double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.0; | |
std::cout << "elapsed = " << elapsed << "[sec]" << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment