summaryrefslogtreecommitdiffhomepage
path: root/benchmark.cpp
blob: 4f5af61ebd386b1102e440c547e840bed3859d57 (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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unordered_map>
#include <c_lib/cstring.h>
#include <c_lib/cmap.h>

declare_CMap(ii, int64_t, int64_t);
declare_CVector_string(s);

#define RAND() rand() * rand()

int main()
{
    clock_t before, difference;
    CMap_ii map = cmap_initializer;
    CMapEntry_ii* entry;
    uint64_t checksum, inserted, erased, get;
    const size_t N = 70000000, seed = 123; // time(NULL);

    printf("Starting %llu\n", N);
    cmap_ii_clear(&map);
    cmap_ii_setMaxLoadFactor(&map, 0.77);
    srand(seed);
    before = clock();
    checksum = inserted = erased = get = 0;
    for (size_t i = 0; i < N; ++i) {
        int64_t rnd = RAND();
        int op = rand() >> 13;
        switch (op) {
        case 1:
            ++inserted;
            checksum += ++cmap_ii_put(&map, rnd, i)->value;
            break;
        case 2:
            entry = cmap_ii_get(map, rnd);
            if (entry) { 
                ++erased;
                checksum += entry->value;
                cmap_ii_erase(&map, rnd);
            }
            break;
        case 3:
            get += (cmap_ii_get(map, rnd) != NULL);
            break;
        }
    }
    difference = clock() - before;
    printf("CMap: sz: %llu, bk: %llu, time: %.02f, sum: %llu, ins: %llu del: %llu\n", cmap_size(map), cmap_buckets(map), (float) difference / CLOCKS_PER_SEC, checksum, inserted, erased);
    cmap_ii_destroy(&map);


    std::unordered_map<int64_t, int64_t> map2;
    std::unordered_map<int64_t, int64_t>::const_iterator iter;
    srand(seed);
    before = clock();
    checksum = inserted = erased = get = 0;
    for (size_t i = 0; i < N; ++i) {
        int64_t rnd = RAND();
        int op = rand() >> 13;
        switch (op) {        
        case 1:
            ++inserted;
            checksum += ++(map2[rnd] = i);
            break;
        case 2:
            iter = map2.find(rnd);
            if (iter != map2.end()) { 
                ++erased;
                checksum += iter->second;
                map2.erase(rnd);
            }
            break;
        case 3:
            get += (map2.find(rnd) != map2.end());
            break;
        }
    }
    difference = clock() - before;
    printf("umap: sz: %llu, bk: %llu, time: %.02f, sum: %llu, ins: %llu del: %llu\n", map2.size(), map2.bucket_count(), (float) difference / CLOCKS_PER_SEC, checksum, inserted, erased);
    map2.clear();
    
    CVector_s names = cvector_initializer;
    cvector_s_push(&names, cstring_make("Robert"));
    cvector_s_push(&names, cstring_make("Johnny"));
    cvector_s_push(&names, cstring_make("Anne"));
    cvector_s_push(&names, cstring_make("Ruth"));
    cvector_s_push(&names, cstring_make("Burt"));
    
    cvector_s_sort(&names);
    c_foreach (i, cvector_s, names)
        printf("%s\n", i.item->str);
    cvector_s_destroy(&names);
}