github.com/scottcagno/storage@v1.8.0/pkg/util/timer.go (about) 1 package util 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 ) 8 9 /* 10 usage: 11 12 func foo() { 13 defer TimeThis(Msg("foo")) 14 // code to measure 15 } 16 17 */ 18 19 func Msg(msg string) (string, time.Time) { 20 return msg, time.Now() 21 } 22 23 func TimeThis(msg string, start time.Time) { 24 log.Printf("%v: %v\n", msg, time.Since(start)) 25 } 26 27 func FormatTime(msg string, t1, t2 time.Time) string { 28 return fmt.Sprintf("%s: %0.6f sec\n", 29 msg, // the message to print 30 float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()), // the seconds 31 ) 32 }