inverse of time.Duration?

276 views
Skip to first unread message

Hemant Singh

unread,
Feb 15, 2019, 2:48:58 PM2/15/19
to golang-nuts
This is an example of time.Duration I have: 5.671msec

I need to convert the duration to rate = 1.0/5.671 msec.

However, time.Duration and float in 1.0 do not mix.  How do I get the rate?

thanks,

Hemant

Michael Jones

unread,
Feb 15, 2019, 2:55:32 PM2/15/19
to Hemant Singh, golang-nuts

start := time.Now()
:
// work to be timed
:
elapsed := time.Now().Sub(start).Seconds()
hertz := 1.0/elapsed

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Michael T. Jones
michae...@gmail.com

Burak Serdar

unread,
Feb 15, 2019, 3:00:33 PM2/15/19
to Hemant Singh, golang-nuts
According to the docs, Duration is nanoseconds in int64. So this should work:

rate=1.0/double(dur)

The result would be in 1/nsecs

Sam Whited

unread,
Feb 15, 2019, 3:15:12 PM2/15/19
to golan...@googlegroups.com
On Fri, Feb 15, 2019, at 20:00, Burak Serdar wrote:
> rate=1.0/double(dur)

nit: s/double/float64/

—Sam

Burak Serdar

unread,
Feb 15, 2019, 3:19:38 PM2/15/19
to Sam Whited, golang-nuts
oops. Thanks for the correction.

>
> —Sam

Hemant Singh

unread,
Feb 15, 2019, 3:51:01 PM2/15/19
to golang-nuts
Thanks all. I figured it out too, but using sprintf and strconv.Atoi.

Best.

Hemant

Louki Sumirniy

unread,
Feb 15, 2019, 3:54:27 PM2/15/19
to golang-nuts
time.Duration is nanoseconds. You can easily get any other unit by dividing by the time.Timeunit by another time unit.(time.Minute time.Second, time.Millisecond, time.Microsecond)

If that 1 in your formula means time.Second then you could just replace it with that, and 5.671ms would be equal to 5671 ns (er, that is if I read it correct as milliseconds, otherwise 5671*time.Millisecond). 

simon place

unread,
Jun 1, 2019, 12:16:47 PM6/1/19
to golang-nuts
just reading this, surprised no mention of this...

d,_:=time.ParseDuration("5.671ms")
Hz:=int(time.Second/d)

Reply all
Reply to author
Forward
0 new messages