diff options
| author | Tyge Løvset <[email protected]> | 2020-12-28 18:18:48 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-12-28 18:18:48 +0100 |
| commit | 74a75b3a8d155ea155358674d3bf8251658183a5 (patch) | |
| tree | 19886986b9ce6943b826311cda40f67ebc8dd7b1 /benchmarks | |
| parent | 83b7be31a1d0fc0be4e013dbfc97bb6cdc3600db (diff) | |
| download | STC-modified-74a75b3a8d155ea155358674d3bf8251658183a5.tar.gz STC-modified-74a75b3a8d155ea155358674d3bf8251658183a5.zip | |
Reverted crand.h API back to rename crand_* to stc64_*, and crand_next() -> stc64_rand().
Diffstat (limited to 'benchmarks')
| -rw-r--r-- | benchmarks/cdeq_benchmark.cpp | 10 | ||||
| -rw-r--r-- | benchmarks/cmap_benchmark.cpp | 6 | ||||
| -rw-r--r-- | benchmarks/cpque_benchmark.cpp | 8 | ||||
| -rw-r--r-- | benchmarks/crand_benchmark.cpp | 138 | ||||
| -rw-r--r-- | benchmarks/crand_benchmark2.cpp | 68 | ||||
| -rw-r--r-- | benchmarks/vector_vs_deque.cpp | 24 |
6 files changed, 163 insertions, 91 deletions
diff --git a/benchmarks/cdeq_benchmark.cpp b/benchmarks/cdeq_benchmark.cpp index fd01ad8b..6a376d2a 100644 --- a/benchmarks/cdeq_benchmark.cpp +++ b/benchmarks/cdeq_benchmark.cpp @@ -4,15 +4,15 @@ #include <stc/cdeq.h>
#include <stc/crand.h>
-enum {N = 200000000, M = 10000, P = 5000, R = 100};
+enum {N = 200000000, M = 10000, P = 5000, R = 50};
using_cdeq(i, int);
void test1() {
clock_t t1 = clock(), t2, t3;
- crand_t rng = crand_init(0);
+ stc64_t rng = stc64_init(0);
std::deque<int> deq;
for (size_t i = 1; i < N; i++) {
- deq.push_front(crand_next(&rng));
+ deq.push_front(stc64_rand(&rng));
if (i % M == 0)
for (int j = 0; j < P; j++)
deq.pop_back();
@@ -30,10 +30,10 @@ void test1() { void test2() {
clock_t t1 = clock(), t2, t3;
- crand_t rng = crand_init(0);
+ stc64_t rng = stc64_init(0);
cdeq_i deq = cdeq_inits;
for (size_t i = 1; i < N; i++) {
- cdeq_i_push_front(&deq, crand_next(&rng));
+ cdeq_i_push_front(&deq, stc64_rand(&rng));
if (i % M == 0)
for (int j = 0; j < P; j++)
cdeq_i_pop_back(&deq);
diff --git a/benchmarks/cmap_benchmark.cpp b/benchmarks/cmap_benchmark.cpp index aaf9fc73..d9b6d1ad 100644 --- a/benchmarks/cmap_benchmark.cpp +++ b/benchmarks/cmap_benchmark.cpp @@ -28,9 +28,9 @@ KHASH_MAP_INIT_INT64(ii, int64_t) size_t seed;
static const float max_load_factor = 0.77f;
-crand_t rng;
-#define SEED(s) rng = crand_init(seed)
-#define RAND(N) (crand_next(&rng) & ((1 << N) - 1))
+stc64_t rng;
+#define SEED(s) rng = stc64_init(seed)
+#define RAND(N) (stc64_rand(&rng) & ((1 << N) - 1))
#define CMAP_SETUP(X, Key, Value) cmap_##X map = cmap_inits \
diff --git a/benchmarks/cpque_benchmark.cpp b/benchmarks/cpque_benchmark.cpp index 625bb056..396d763d 100644 --- a/benchmarks/cpque_benchmark.cpp +++ b/benchmarks/cpque_benchmark.cpp @@ -10,15 +10,15 @@ using_cpque(f, cvec_f, >); int main()
{
uint32_t seed = time(NULL);
- crand_t rng;
+ stc64_t rng;
int N = 10000000, M = 10;
cpque_f pq = cpque_f_init();
- rng = crand_init(seed);
+ rng = stc64_init(seed);
clock_t start = clock();
c_forrange (i, int, N)
- cvec_f_push_back(&pq, (float) crand_nextf(&rng)*100000);
+ cvec_f_push_back(&pq, (float) stc64_randf(&rng)*100000);
cpque_f_make_heap(&pq);
printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
@@ -35,7 +35,7 @@ int main() start = clock();
c_forrange (i, int, N)
- cpque_f_push(&pq, (float) crand_nextf(&rng)*100000);
+ cpque_f_push(&pq, (float) stc64_randf(&rng)*100000);
printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
c_forrange (i, int, M) {
diff --git a/benchmarks/crand_benchmark.cpp b/benchmarks/crand_benchmark.cpp index 78099905..70a3bb45 100644 --- a/benchmarks/crand_benchmark.cpp +++ b/benchmarks/crand_benchmark.cpp @@ -4,15 +4,15 @@ #include <stc/crand.h>
static inline uint64_t rotl64(const uint64_t x, const int k)
- { return (x << k) | (x >> (64 - k)); }
+ { return (x << k) | (x >> (64 - k)); }
static uint64_t splitmix64_x = 87213627321ull; /* The state can be seeded with any value. */
uint64_t splitmix64(void) {
- uint64_t z = (splitmix64_x += 0x9e3779b97f4a7c15);
- z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
- z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
- return z ^ (z >> 31);
+ uint64_t z = (splitmix64_x += 0x9e3779b97f4a7c15);
+ z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
+ z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
+ return z ^ (z >> 31);
}
static void init_state(uint64_t *rng, uint64_t seed) {
@@ -23,26 +23,47 @@ static void init_state(uint64_t *rng, uint64_t seed) { /* jsf64 */
static inline uint64_t jsf64(uint64_t *s) {
- uint64_t e = s[0] - rotl64(s[1], 7);
- s[0] = s[1] ^ rotl64(s[2], 13);
- s[1] = s[2] + rotl64(s[3], 37);
- s[2] = s[3] + e;
- s[3] = e + s[0];
- return s[3];
+ uint64_t e = s[0] - rotl64(s[1], 7);
+ s[0] = s[1] ^ rotl64(s[2], 13);
+ s[1] = s[2] + rotl64(s[3], 37);
+ s[2] = s[3] + e;
+ s[3] = e + s[0];
+ return s[3];
}
+/* sfc64 */
+
+static inline uint64_t sfc64(uint64_t *s) {
+ uint64_t result = s[0] + s[1] + s[3]++;
+ s[0] = s[1] ^ (s[1] >> 11);
+ s[1] = s[2] + (s[2] << 3);
+ s[2] = rotl64(s[2], 24) + result;
+ return result;
+}
+
+/* sfc64 with Weyl increment */
+static uint64_t weyl = 1234566789123ull;
+static inline uint64_t sfc64w(uint64_t *s) {
+ uint64_t result = s[0] + s[1] + (s[3] += weyl|1);
+ s[0] = s[1] ^ (s[1] >> 11);
+ s[1] = s[2] + (s[2] << 3);
+ s[2] = rotl64(s[2], 24) + result;
+ return result;
+}
+
+
/* xoshiro256** */
static inline uint64_t xoshiro256starstar(uint64_t* s) {
- const uint64_t result = rotl64(s[1] * 5, 7) * 9;
- const uint64_t t = s[1] << 17;
- s[2] ^= s[0];
- s[3] ^= s[1];
- s[1] ^= s[2];
- s[0] ^= s[3];
- s[2] ^= t;
- s[3] = rotl64(s[3], 45);
- return result;
+ const uint64_t result = rotl64(s[1] * 5, 7) * 9;
+ const uint64_t t = s[1] << 17;
+ s[2] ^= s[0];
+ s[3] ^= s[1];
+ s[1] ^= s[2];
+ s[0] ^= s[3];
+ s[2] ^= t;
+ s[3] = rotl64(s[3], 45);
+ return result;
}
@@ -63,92 +84,103 @@ using namespace std; int main(void)
{
- enum {N = 524288000};
+ enum {N = 1000000000};
uint64_t* recipient = new uint64_t[N];
- static crand_t rng;
+ static stc64_t rng;
init_state(rng.state, 12345123);
+ cout << "WARMUP" << endl;
+ for (size_t i = 0; i < N; i++)
+ recipient[i] = wyhash64(rng.state);
+
clock_t beg, end;
for (size_t ti = 0; ti < 4; ti++) {
+ cout << endl << "ROUND " << ti+1 << endl;
beg = clock();
for (size_t i = 0; i < N; i++)
recipient[i] = wyhash64(rng.state);
end = clock();
- cerr << "ROUND " << ti+1 << endl
- << "wyhash64:\t"
+ cout << "wyhash64:\t"
<< (float(end - beg) / CLOCKS_PER_SEC)
- << " s" << endl;
- cout << "bogus:" << recipient[312] << endl;
+ << " s: " << recipient[312] << endl;
beg = clock();
for (size_t i = 0; i < N; i++)
- recipient[i] = crand_next(&rng);
+ recipient[i] = sfc64w(rng.state);
end = clock();
- cerr << "stc crand:\t"
+ cout << "sfc64w:\t\t"
<< (float(end - beg) / CLOCKS_PER_SEC)
- << " s" << endl;
- cout << "bogus:" << recipient[312] << endl;
+ << " s: " << recipient[312] << endl;
+
+ beg = clock();
+ for (size_t i = 0; i < N; i++)
+ recipient[i] = stc64_rand(&rng);
+ end = clock();
+ cout << "stc-crand:\t"
+ << (float(end - beg) / CLOCKS_PER_SEC)
+ << " s: " << recipient[312] << endl;
beg = clock();
for (size_t i = 0; i < N; i++)
recipient[i] = xoshiro256starstar(rng.state);
end = clock();
- cerr << "xoshiro256**:\t"
+ cout << "xoshiro256**:\t"
<< (float(end - beg) / CLOCKS_PER_SEC)
- << " s" << endl;
- cout << "bogus:" << recipient[312] << endl;
+ << " s: " << recipient[312] << endl;
beg = clock();
for (size_t i = 0; i < N; i++)
recipient[i] = lehmer64(rng.state);
end = clock();
- cerr << "lehmer64:\t"
+ cout << "lehmer64:\t"
<< ((float) end - beg) / CLOCKS_PER_SEC
- << " s" << endl;
- cout << "bogus:" << recipient[312] << endl;
-
-
+ << " s: " << recipient[312] << endl;
- cout << endl
- << "Next we do random number computations only, doing no work."
+ cout << "Next we do random number computations only, doing no work."
<< endl;
uint64_t s = 0;
beg = clock();
for (size_t i = 0; i < N; i++)
s += wyhash64(rng.state);
end = clock();
- cerr << "wyhash64:\t"
+ cout << "wyhash64:\t"
+ << ((float) end - beg) / CLOCKS_PER_SEC
+ << " s: " << s << endl;
+
+ s = 0;
+ beg = clock();
+ for (size_t i = 0; i < N; i++)
+ s += sfc64w(rng.state);
+ end = clock();
+ cout << "sfc64w:\t\t"
<< ((float) end - beg) / CLOCKS_PER_SEC
- << " s" << endl;
- cout << "bogus:" << s << endl;
+ << " s: " << s << endl;
s = 0;
beg = clock();
for (size_t i = 0; i < N; i++)
- s += crand_next(&rng);
+ s += stc64_rand(&rng);
end = clock();
- cerr << "stc crand:\t"
+ cout << "stc-crand:\t"
<< ((float) end - beg) / CLOCKS_PER_SEC
- << " s" << endl;
- cout << "bogus:" << s << endl;
+ << " s: " << s << endl;
s = 0;
beg = clock();
for (size_t i = 0; i < N; i++)
s += xoshiro256starstar(rng.state);
end = clock();
- cerr << "xoshiro256**:\t"
+ cout << "xoshiro256**:\t"
<< ((float) end - beg) / CLOCKS_PER_SEC
- << " s" << endl;
- cout << "bogus:" << s << endl;
+ << " s: " << s << endl;
+ s = 0;
beg = clock();
for (size_t i = 0; i < N; i++)
s += lehmer64(rng.state);
end = clock();
- cerr << "lehmer64:\t"
+ cout << "lehmer64:\t"
<< ((float) end - beg) / CLOCKS_PER_SEC
- << " s" << endl;
- cout << "bogus:" << s << endl << endl;
+ << " s: " << s << endl;
}
delete[] recipient;
return 0;
diff --git a/benchmarks/crand_benchmark2.cpp b/benchmarks/crand_benchmark2.cpp index ac7296fc..966a3675 100644 --- a/benchmarks/crand_benchmark2.cpp +++ b/benchmarks/crand_benchmark2.cpp @@ -1,11 +1,13 @@ #include <stdio.h>
#include <time.h>
-#include <stc/crand.h>
#include <random>
+#include "stc/crand.h"
+#include "others/pcg_random.hpp"
+
+enum {N = 1000000000};
void test1(void)
{
- enum {N = 1000000000};
clock_t diff, before;
uint64_t sum;
@@ -20,7 +22,7 @@ void test1(void) sum += rng();
}
diff = clock() - before;
- printf("std::random:\t\t%.02f, %zu\n", (float) diff / CLOCKS_PER_SEC, sum);
+ printf("std::random:\t\t%.02f, %zu, sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng);
before = clock();
sum = 0;
@@ -36,35 +38,72 @@ void test1(void) puts("\n");
}
-void test2(void)
+void test2()
+{
+ clock_t diff, before;
+ uint64_t sum;
+
+ // Seed with a real random value, if available
+ pcg_extras::seed_seq_from<std::random_device> seed_source;
+
+ // Make a random number engine
+ pcg64 rng(seed_source);
+
+ // Choose a random mean between 1 and 10
+ std::uniform_int_distribution<int> idist(1, 10);
+ std::uniform_real_distribution<double> fdist(1, 10);
+
+ before = clock();
+ sum = 0;
+ c_forrange (N) {
+ sum += rng();
+ }
+ diff = clock() - before;
+ printf("pcg64::random:\t\t%.02f, %zu, sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng);
+
+ before = clock();
+ sum = 0;
+ c_forrange (N) {
+ sum += idist(rng);
+ }
+ diff = clock() - before;
+ printf("pcg64::uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum);
+
+ c_forrange (30) printf("%02d ", idist(rng));
+ puts("");
+ c_forrange (8) printf("%f ", fdist(rng));
+ puts("\n");
+}
+
+
+void test3(void)
{
- enum {N = 1000000000};
clock_t diff, before;
uint64_t sum;
- crand_t rng = crand_init(time(NULL));
- crand_uniform_t idist = crand_uniform_init(1, 10);
- crand_uniformf_t fdist = crand_uniformf_init(1, 10);
+ stc64_t rng = stc64_init(time(NULL));
+ stc64_uniform_t idist = stc64_uniform_init(1, 10);
+ stc64_uniformf_t fdist = stc64_uniformf_init(1, 10);
before = clock();
sum = 0;
c_forrange (N) {
- sum += crand_next(&rng);
+ sum += stc64_rand(&rng);
}
diff = clock() - before;
- printf("crand_next:\t\t%.02f, %zu\n", (float) diff / CLOCKS_PER_SEC, sum);
+ printf("stc64_random:\t\t%.02f, %zu sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng);
before = clock();
sum = 0;
c_forrange (N) {
- sum += crand_uniform(&rng, &idist);
+ sum += stc64_uniform(&rng, &idist);
}
diff = clock() - before;
- printf("crand_uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum);
+ printf("stc64_uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum);
- c_forrange (30) printf("%02zd ", crand_uniform(&rng, &idist));
+ c_forrange (30) printf("%02zd ", stc64_uniform(&rng, &idist));
puts("");
- c_forrange (8) printf("%f ", crand_uniformf(&rng, &fdist));
+ c_forrange (8) printf("%f ", stc64_uniformf(&rng, &fdist));
puts("\n");
}
@@ -72,4 +111,5 @@ int main() {
test1();
test2();
+ test3();
}
\ No newline at end of file diff --git a/benchmarks/vector_vs_deque.cpp b/benchmarks/vector_vs_deque.cpp index ae37f246..6c26a823 100644 --- a/benchmarks/vector_vs_deque.cpp +++ b/benchmarks/vector_vs_deque.cpp @@ -20,7 +20,7 @@ void add(cvec_si* tm, const char* s, int n) { Si si = {s, n}; cvec_si_push_back( void test_vector(const int num_iterations)
{
std::vector<int> v;
- crand_t rng = crand_init(0);
+ stc64_t rng = stc64_init(0);
v.reserve(num_iterations + 2); //Ensure there is enough space reserved.
// == PUSH_BACK
@@ -29,7 +29,7 @@ void test_vector(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
- v.push_back(crand_next(&rng));
+ v.push_back(stc64_rand(&rng));
}
clock_t t2 = std::clock();
@@ -44,7 +44,7 @@ void test_vector(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
// Rather add some more elements to back.
- v.push_back(crand_next(&rng));
+ v.push_back(stc64_rand(&rng));
}
clock_t t2 = std::clock();
@@ -96,7 +96,7 @@ void test_vector(const int num_iterations) void test_deque(const int num_iterations)
{
std::deque<int> d;
- crand_t rng = crand_init(0);
+ stc64_t rng = stc64_init(0);
// == PUSH_BACK
{
@@ -104,7 +104,7 @@ void test_deque(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
- d.push_back(crand_next(&rng));
+ d.push_back(stc64_rand(&rng));
}
clock_t t2 = std::clock();
@@ -118,7 +118,7 @@ void test_deque(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
- d.push_front(crand_next(&rng));
+ d.push_front(stc64_rand(&rng));
}
clock_t t2 = std::clock();
@@ -177,7 +177,7 @@ using_cvec(i, int); void test_cvec(const int num_iterations)
{
cvec_i v = cvec_inits;
- crand_t rng = crand_init(0);
+ stc64_t rng = stc64_init(0);
//v.reserve(num_iterations + 2); //Ensure there is enough space reserved.
// == PUSH_BACK
@@ -186,7 +186,7 @@ void test_cvec(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
- cvec_i_push_back(&v, crand_next(&rng));
+ cvec_i_push_back(&v, stc64_rand(&rng));
}
clock_t t2 = std::clock();
@@ -201,7 +201,7 @@ void test_cvec(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
// Rather add some more elements to back.
- cvec_i_push_back(&v, crand_next(&rng));
+ cvec_i_push_back(&v, stc64_rand(&rng));
}
clock_t t2 = std::clock();
@@ -257,7 +257,7 @@ using_cdeq(i, int); void test_cdeq(const int num_iterations)
{
cdeq_i d = cdeq_i_with_capacity(num_iterations + 2);
- crand_t rng = crand_init(0);
+ stc64_t rng = stc64_init(0);
// == PUSH_BACK
{
@@ -265,7 +265,7 @@ void test_cdeq(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
- cdeq_i_push_back(&d, crand_next(&rng));
+ cdeq_i_push_back(&d, stc64_rand(&rng));
}
clock_t t2 = std::clock();
@@ -279,7 +279,7 @@ void test_cdeq(const int num_iterations) for (int i=0; i<(num_iterations>>1); ++i)
{
- cdeq_i_push_front(&d, crand_next(&rng));
+ cdeq_i_push_front(&d, stc64_rand(&rng));
}
clock_t t2 = std::clock();
|
