forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.go
43 lines (38 loc) · 931 Bytes
/
timing.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
package profile
import (
"fmt"
"time"
)
var (
uptime int64
times = make(map[string]int64)
activeTimers = make(map[string]int64)
)
// Uptime() returns the number of nanoseconds that have passed since the first
// call to uptime.
func Uptime() int64 {
if uptime == 0 {
uptime = time.Now().UnixNano()
return 0
}
return (time.Now().UnixNano() - uptime) / 1e6
}
// PrintTimes prints how much time has passed at each timer.
func PrintTimes() string {
s := "Printing Timers:\n"
for name, time := range times {
s += fmt.Sprintf("\t%v: %v\n", name, time/1e6)
}
return s
}
// ToggleTimer actives a timer known by a given string. If the timer does not
// yet exist, it is created.
func ToggleTimer(s string) {
toggleTime, exists := activeTimers[s]
if exists {
times[s] = times[s] + (time.Now().UnixNano() - toggleTime)
delete(activeTimers, s)
} else {
activeTimers[s] = time.Now().UnixNano()
}
}