-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmap_bm.cc
76 lines (66 loc) · 1.68 KB
/
map_bm.cc
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
// Benchmarks map<string, int>.
//
// To benchmark unordered_map:
//
// $ sed 's/\<map\>/unordered_map/g' map_bm.cc > umap_bm.cc
// $ g++ -O3 -std=gnu++11 umap_bm.cc -ltcmalloc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <map>
#include <string>
#include <vector>
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;
static struct timespec bm_tp[2];
void bm_init() {
clock_gettime(CLOCK_MONOTONIC, bm_tp);
}
void bm_report(const string& msg) {
clock_gettime(CLOCK_MONOTONIC, bm_tp + 1);
printf("%s: %ld.%09lds\n", msg.c_str(),
bm_tp[1].tv_sec - bm_tp[0].tv_sec - (bm_tp[1].tv_nsec < bm_tp[0].tv_nsec),
bm_tp[1].tv_nsec - bm_tp[0].tv_nsec
+ (bm_tp[1].tv_nsec < bm_tp[0].tv_nsec) * 1000000000L);
clock_gettime(CLOCK_MONOTONIC, bm_tp);
}
int main() {
vector<string> key;
int m = 0;
for (;;) {
char *s = 0;
size_t n;
ssize_t len = getline(&s, &n, stdin);
if (feof(stdin)) break;
if (len == -1) perror("getline"), exit(1);
if (s[len - 1] == '\n') s[len - 1] = 0;
key.push_back(s);
m++;
}
// Randomize order of array.
for (int i = m-1; i>1; i--) {
int j = random() % i;
string tmp = key[i];
key[i] = key[j];
key[j] = tmp;
}
map<string, int> smap;
bm_init();
REP(i, m) smap[key[i]] = i;
bm_report("map insert");
REP(i, m) if (i != smap[key[i]]) {
fprintf(stderr, "BUG!\n");
exit(1);
}
bm_report("map get");
int count = 0;
for (map<string, int>::iterator it = smap.begin(); it != smap.end(); it++) count++;
if (count != m) {
fprintf(stderr, "BUG!\n");
exit(1);
}
bm_report("map iterate");
smap.clear();
bm_report("map delete");
}