Calculate Standard Errors on time.Duration


(FREDERIC FLAMENT) #1

I need to compute some (elementary) statistics on durations. Although I like time implementation in Go and i need the duration functions to determine a time lapse, and I could add and multiply (although not elegantly) durations, I find myself in a dead end when i want to compute the Square Root. The only solution I see is to convert to float64 (But how convert any time lapse in float64 ?)

Thank you


(Yamil Bracho) #2

A time.Duration is based in int64
type Duration [int64]
So you can convert to float justo doing a cast operation to float64. For example

package main

import (
	"fmt"
	"time"
)

func expensiveCall() {
   time.Sleep(100 * time.Millisecond)
}

func main() {
	t0 := time.Now()
	expensiveCall()
	t1 := time.Now()
	
	elapsed := t1.Sub(t0)
	fmt.Printf("The call took %v to run.\n", elapsed)
	
	fmt.Printf("In float=%f", float64(elapsed))
}

(FREDERIC FLAMENT) #3

Thank you. The trick is to cast duration directly into float64. And recast into time.Duration when the computation is done.


(Jakob Borg) #4

Or, use the Seconds() method to get a reasonable unit as a float64.