-
Notifications
You must be signed in to change notification settings - Fork 2
/
bench.go
84 lines (71 loc) · 1.92 KB
/
bench.go
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
// Copyright (c) 2010, Jonathan Wills ([email protected])
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "rand"
import "time"
import "fmt"
import "flag"
var N int
var R int
func init() {
flag.IntVar(&N, "size", 1000000, "Size of input arrays")
flag.IntVar(&R, "runs", 10, "Number of times to repeat each test")
flag.Parse()
}
func Bench(d []int) []float {
times := make([]float, 5)
t := NewTree(func(a,b int) bool { return a < b })
start := time.Nanoseconds()
for _,v := range d {
t.Insert(v)
}
times[0] = float(time.Nanoseconds() - start) / 1000000000.0
start = time.Nanoseconds()
for _,v := range d {
t.Insert(v)
}
times[1] = float(time.Nanoseconds() - start) / 1000000000.0
start = time.Nanoseconds()
for i := 0; i < len(d)/2; i++ {
t.Remove(d[i])
}
times[2] = float(time.Nanoseconds() - start) / 1000000000.0
start = time.Nanoseconds()
for i := 0; i < len(d)/2; i++ {
t.Remove(d[i])
}
times[3] = float(time.Nanoseconds() - start) / 1000000000.0
start = time.Nanoseconds()
for v := range d {
t.Contains(v)
}
times[4] = float(time.Nanoseconds() - start) / 1000000000.0
return times
}
func main() {
names := []string {
"Unique Inserts",
"Repeated Inserts",
"Unique Deletes",
"Repeated Deletes",
"Queries",
}
d := rand.Perm(N)
total := Bench(d)
for i := 1; i < R; i++ {
times := Bench(d)
for j := range times {
total[j] += times[j]
}
}
for i := range total {
total[i] /= float(R)
}
fmt.Printf("Using input size %d and averaged over %d runs.\n", N, R)
fmt.Printf("%3.3f:\t%d\t%s\n", total[0], N, names[0])
fmt.Printf("%3.3f:\t%d\t%s\n", total[1], N, names[1])
fmt.Printf("%3.3f:\t%d\t%s\n", total[2], N/2, names[2])
fmt.Printf("%3.3f:\t%d\t%s\n", total[3], N/2, names[3])
fmt.Printf("%3.3f:\t%d\t%s\n", total[4], N, names[4])
}