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
| package util
import ( "time" )
type Timer struct { Started int64 `json:"started"` Completed int64 `json:"complete"` }
func TimerStart() *Timer { return &Timer{Started: TimeCurrentNanos()} }
func (t *Timer) End() int { t.Completed = TimeCurrentNanos() return t.Elapsed() }
func (t *Timer) EndString() string { t.End() return t.String() }
func (t *Timer) Elapsed() int { if t.Completed == 0 { return int((TimeCurrentNanos() - t.Started) / int64(time.Microsecond)) } return int((t.Completed - t.Started) / int64(time.Microsecond)) }
func (t *Timer) String() string { return MicrosToMillis(t.Elapsed()) }
|