added C++ measurements
This commit is contained in:
parent
40da271e34
commit
a6dd3f93cd
34
perf.cpp
34
perf.cpp
@ -2,6 +2,7 @@
|
||||
#include <cstdlib>
|
||||
#include <chrono>
|
||||
#include <cassert>
|
||||
#include <random>
|
||||
#include "fastrand.h"
|
||||
|
||||
#define N 10000000
|
||||
@ -22,6 +23,10 @@ int main() {
|
||||
srand((unsigned int)time(NULL));
|
||||
rand_state rs = init_rand();
|
||||
rand_ilp_state rs_ilp = init_rand_ilp();
|
||||
// C++ engines
|
||||
std::linear_congruential_engine<uint32_t, 1664525u, 1013904223u, 0> lce;
|
||||
std::mt19937 mte;
|
||||
std::minstd_rand lce_def;
|
||||
|
||||
// Generate FROM,TO as random, because otherwise compiler optimizes out IDIV of the '%' operator!
|
||||
uint32_t FROM = (uint32_t) rand();
|
||||
@ -45,6 +50,27 @@ int main() {
|
||||
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// C++ LCG
|
||||
for (int i = 0; i < N; ++i) {
|
||||
res[i] += lce_def();
|
||||
}
|
||||
|
||||
auto t21 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// C++ LCG - my parameters
|
||||
for (int i = 0; i < N; ++i) {
|
||||
res[i] += lce();
|
||||
}
|
||||
|
||||
auto t211 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// C++ MT
|
||||
for (int i = 0; i < N; ++i) {
|
||||
res[i] += mte();
|
||||
}
|
||||
|
||||
auto t22 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// lcg
|
||||
for (int i = 0; i < N; ++i) {
|
||||
res[i] += lcg(&rs);
|
||||
@ -65,11 +91,17 @@ int main() {
|
||||
|
||||
auto arc4_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0);
|
||||
auto rand_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1);
|
||||
auto lcg_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t3 - t2);
|
||||
auto lce_def_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t21 - t2);
|
||||
auto lce_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t211 - t21);
|
||||
auto mt_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t22 - t21);
|
||||
auto lcg_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t3 - t22);
|
||||
auto lcg4_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t31 - t3);
|
||||
|
||||
printf("Time (arc4): %.3f ms.\n", arc4_elapsed.count() * 1e-6);
|
||||
printf("Time (rand): %.3f ms.\n", rand_elapsed.count() * 1e-6);
|
||||
printf("Time (C++ lcg): %.3f ms.\n", lce_def_elapsed.count() * 1e-6);
|
||||
printf("Time (C++ lcg my parameters): %.3f ms.\n", lce_elapsed.count() * 1e-6);
|
||||
printf("Time (C++ mersenne twister 32bit): %.3f ms.\n", mt_elapsed.count() * 1e-6);
|
||||
printf("Time (lcg): %.3f ms.\n", lcg_elapsed.count() * 1e-6);
|
||||
printf("Time (lcg4): %.3f ms.\n", lcg4_elapsed.count() * 1e-6);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user