summaryrefslogtreecommitdiffhomepage
path: root/benchmarks/crand_benchmark2.cpp
blob: 966a367529b78cc94835bbd00e93ffd3e1b5107d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <time.h>
#include <random>
#include "stc/crand.h"
#include "others/pcg_random.hpp"

enum {N = 1000000000};

void test1(void)
{
    clock_t diff, before;
    uint64_t sum;

    std::random_device device;
    std::mt19937 rng(device());
    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("std::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("std::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 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)
{
    clock_t diff, before;
    uint64_t sum;

    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 += stc64_rand(&rng);
    }
    diff = clock() - before;
    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 += stc64_uniform(&rng, &idist);
    }
    diff = clock() - before;
    printf("stc64_uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum);

    c_forrange (30) printf("%02zd ", stc64_uniform(&rng, &idist));
    puts("");
    c_forrange (8) printf("%f ", stc64_uniformf(&rng, &fdist));
    puts("\n");
}

int main()
{
    test1();
    test2();
    test3();
}