2025-04-01 20:01:12 +02:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <chrono>
|
2025-04-01 20:06:11 +02:00
|
|
|
#include <cassert>
|
2025-04-01 20:01:12 +02:00
|
|
|
#include "fastrand.h"
|
|
|
|
|
|
2025-04-01 20:06:11 +02:00
|
|
|
#define N 10000000
|
2025-04-01 20:25:53 +02:00
|
|
|
// #define N 19999999
|
|
|
|
|
// #define M 10000000 // M >= N
|
|
|
|
|
#define M 19999999 // M >= N
|
2025-04-01 20:01:12 +02:00
|
|
|
#define FROM 100
|
|
|
|
|
#define TO 576
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
uint32_t res[M] = { 0 };
|
|
|
|
|
|
2025-04-01 20:01:12 +02:00
|
|
|
int main() {
|
2025-04-01 20:25:53 +02:00
|
|
|
assert(M >= N); // M >= N
|
2025-04-01 20:06:11 +02:00
|
|
|
|
2025-04-01 20:01:12 +02:00
|
|
|
// Init
|
|
|
|
|
srand((unsigned int)time(NULL));
|
|
|
|
|
rand_state rs = init_rand();
|
|
|
|
|
|
|
|
|
|
printf("Full range generation perf - %d number of cases:\n", N);
|
|
|
|
|
|
|
|
|
|
auto t0 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
// arc4
|
2025-04-01 20:01:12 +02:00
|
|
|
for (int i = 0; i < N; ++i) {
|
2025-04-01 20:25:53 +02:00
|
|
|
res[i] += arc4random();
|
2025-04-01 20:01:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto t1 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
// rand
|
2025-04-01 20:01:12 +02:00
|
|
|
for (int i = 0; i < N; ++i) {
|
2025-04-01 20:25:53 +02:00
|
|
|
res[i] += rand();
|
2025-04-01 20:01:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto t2 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
// lcg
|
|
|
|
|
for (int i = 0; i < N; ++i) {
|
2025-04-01 20:25:53 +02:00
|
|
|
res[i] += lcg(&rs);
|
2025-04-01 20:01:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto t3 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
// results 1
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
auto arc4_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0);
|
|
|
|
|
auto rand_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1);
|
2025-04-01 20:01:12 +02:00
|
|
|
auto lcg_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t3 - t2);
|
|
|
|
|
|
|
|
|
|
printf("Time (arc4): %.3f ms.\n", arc4_elapsed.count() * 1e-6);
|
2025-04-01 20:25:53 +02:00
|
|
|
printf("Time (rand): %.3f ms.\n", rand_elapsed.count() * 1e-6);
|
2025-04-01 20:01:12 +02:00
|
|
|
printf("Time (lcg): %.3f ms.\n", lcg_elapsed.count() * 1e-6);
|
|
|
|
|
|
|
|
|
|
printf("Modulo VS nomod perf for rand_between (both LCG) - %d number of cases:\n", M);
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
|
2025-04-01 20:01:12 +02:00
|
|
|
auto t4 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
// rand + modulo
|
2025-04-01 20:06:11 +02:00
|
|
|
for (int i = 0; i < M; ++i) {
|
2025-04-01 20:25:53 +02:00
|
|
|
res[i] += FROM + (rand() % (TO - FROM));
|
2025-04-01 20:01:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto t5 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
// lcg + modulo
|
2025-04-01 20:06:11 +02:00
|
|
|
for (int i = 0; i < M; ++i) {
|
2025-04-01 20:25:53 +02:00
|
|
|
res[i] += FROM + (lcg(&rs) % (TO - FROM));
|
2025-04-01 20:01:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto t6 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
// rand_between (also LCG, but no modulus)
|
|
|
|
|
for (int i = 0; i < M; ++i) {
|
|
|
|
|
res[i] += rand_between(&rs, FROM, TO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto t7 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
|
2025-04-01 20:01:12 +02:00
|
|
|
// results 2
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
auto randmod_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t5 - t4);
|
|
|
|
|
auto mod_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t6 - t5);
|
|
|
|
|
auto between_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(t7 - t6);
|
2025-04-01 20:01:12 +02:00
|
|
|
|
|
|
|
|
uint32_t choice = rand_between(&rs, FROM, TO);
|
2025-04-01 20:25:53 +02:00
|
|
|
printf("rand + modulo [%u, %u): %.3f ms.\n", FROM, TO, randmod_elapsed.count() * 1e-6);
|
2025-04-01 20:01:12 +02:00
|
|
|
printf("lcg + modulo [%u, %u): %.3f ms.\n", FROM, TO, mod_elapsed.count() * 1e-6);
|
|
|
|
|
printf("rand_between [%u, %u): %.3f ms.\n", FROM, TO, between_elapsed.count() * 1e-6);
|
|
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
// checksum - avoids optimizing out above loops
|
2025-04-01 20:01:12 +02:00
|
|
|
|
2025-04-01 20:25:53 +02:00
|
|
|
uint32_t sum = 0;
|
|
|
|
|
for(int i = 0; i < M; ++i) {
|
|
|
|
|
sum += res[i];
|
|
|
|
|
}
|
2025-04-01 20:01:12 +02:00
|
|
|
printf("Checksum: 0x%x\n", sum);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|