-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.go
48 lines (40 loc) · 1.17 KB
/
benchmark.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
package benchmark
import (
"fmt"
"time"
)
// Benchmark struct
type BM struct {
startTime time.Time
endTime time.Time
duration time.Duration
}
// Start a new benchmark
func (b *BM) Start() {
b.startTime = time.Now()
}
// End the benchmark then record the duration
func (b *BM) End() {
b.endTime = time.Now()
b.duration = b.endTime.Sub(b.startTime)
}
// Display the recorded duration in nanoseconds
func (b *BM) DurationInNanoseconds(label string) {
durationInNs := float64(b.duration.Nanoseconds())
fmt.Printf("%v %fns\n", label, durationInNs)
}
// Display the recorded duration in microseconds
func (b *BM) DurationInMicroseconds(label string) {
durationInUs := float64(b.duration.Nanoseconds()) * 0.001
fmt.Printf("%v %fµs\n", label, durationInUs)
}
// Display the recorded duration in milliseconds
func (b *BM) DurationInMilliseconds(label string) {
durationInMs := float64(b.duration.Nanoseconds()) * 0.000001
fmt.Printf("%v %fms\n", label, durationInMs)
}
// Display the recorded duration in seconds
func (b *BM) DurationInSeconds(label string) {
durationInSecs := float64(b.duration.Nanoseconds()) * 0.000000001
fmt.Printf("%v %fs\n", label, durationInSecs)
}