-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.v
151 lines (138 loc) · 2.87 KB
/
benchmark.v
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import hashmap
import btree
import rand
import time
fn hashmap_set_bench(arr []string, repeat int) {
start_time := time.ticks()
for _ in 0..repeat {
mut b := hashmap.new_hmap()
for x in arr {
b.set(x, 1)
}
}
end_time := time.ticks() - start_time
string_len := arr[0].len
println("hashmap_set_bench_$string_len: $end_time")
}
fn btree_set_bench(arr []string, repeat int) {
start_time := time.ticks()
for _ in 0..repeat {
mut b := btree.new_tree()
for x in arr {
b.set(x, 1)
}
}
end_time := time.ticks() - start_time
string_len := arr[0].len
println("btree_set_bench_$string_len: $end_time")
}
fn map_set_bench(arr []string, repeat int) {
start_time := time.ticks()
for _ in 0..repeat {
mut b := map[string]int
for x in arr {
b[x] = 1
}
}
end_time := time.ticks() - start_time
string_len := arr[0].len
println("map_set_bench_$string_len: $end_time")
}
fn hashmap_get_bench(arr []string, repeat int) {
mut b := hashmap.new_hmap()
for x in arr {
b.set(x, 1)
}
mut a := 0
start_time := time.ticks()
for _ in 0..repeat {
for x in arr {
a += b.get(x)
}
}
end_time := time.ticks() - start_time
string_len := arr[0].len
println("hashmap_get_bench_$string_len: $end_time")
}
fn btree_get_bench(arr []string, repeat int) {
mut b := btree.new_tree()
for x in arr {
b.set(x, 1)
}
start_time := time.ticks()
for _ in 0..repeat {
for x in arr {
b.get(x)
}
}
end_time := time.ticks() - start_time
string_len := arr[0].len
println("btree_get_bench_$string_len: $end_time")
}
fn map_get_bench(arr []string, repeat int) {
mut b := map[string]int
for x in arr {
b[x] = 1
}
start_time := time.ticks()
for _ in 0..repeat {
for x in arr {
b[x]
}
}
end_time := time.ticks() - start_time
string_len := arr[0].len
println("map_get_bench_$string_len: $end_time")
}
fn benchmark_strings() {
for i := 1; i < 1025; i = i * 2 {
mut arr := []string
for _ in 0..10000 {
mut buf := []byte
for j in 0..i {
buf << byte(rand.next(int(`z`) - int(`a`)) + `a`)
}
s := string(buf)
arr << s
}
// map_set_bench(arr, 100)
hashmap_set_bench(arr, 100)
// map_get_bench(arr, 100)
// hashmap_get_bench(arr, 100)
}
}
fn benchmark_size() {
for i := 1; i < 10000; i = i * 2 {
mut arr := []string
for _ in 0..(10 * i) {
mut buf := []byte
for j in 0..15 {
buf << byte(rand.next(int(`z`) - int(`a`)) + `a`)
}
s := string(buf)
arr << s
}
println(arr.len)
// map_set_bench(arr, 1000)
// hashmap_set_bench(arr, 10)
// btree_set_bench(arr, 1000)
// map_get_bench(arr, 1000)
hashmap_get_bench(arr, 10000)
// btree_get_bench(arr, 1000)
}
}
fn benchmark_nums() {
mut arr := []string
for i in 0..10000 {
arr << i.str()
}
map_get_bench(arr, 10)
// hashmap_get_bench(arr, 10)
map_set_bench(arr, 10)
hashmap_set_bench(arr, 10)
}
fn main() {
// benchmark_strings()
// benchmark_nums()
benchmark_size()
}