benchmark and start/stop timer

605 views
Skip to first unread message

jonathan...@gmail.com

unread,
Apr 25, 2016, 4:11:34 PM4/25/16
to golang-nuts
I have a test that has some init that takes a few seconds like:

func Benchmark(b *testing.B) {
    b.StopTimer()

    //long init

    b.StartTime()

    for i := 0; i < b.N; i++ {
        //func that takes around 50ms
    }
}

and what I am seeing is that if -benchtime is at 1s then I get a ns/op time that is above 1 second, which is not correct?

However the higher I put -benchtime the more and more the ns/op approaches the correct 50ms time.

If I put the b.StopTimer()/b.StartTime() around the func in the iteration loop, it always gives the correct ns/op time. However
as far as I understand I can't use that kind of thing for b.RunParallel().

So what is the correct usage and expected behavior and what to do with b.RunParallel()?

Michael Jones

unread,
Apr 25, 2016, 4:27:36 PM4/25/16
to jonathan...@gmail.com, golang-nuts
I had some strangeness with start and stop timer in the benchmark package a month ago. Sorry to admit that I did not track it down.

— 
Michael Jones, CEO  •  mic...@wearality.com  •  +1 650 656-6989 
Wearality Corporation  •  289 S. San Antonio Road  •  Los Altos, CA 94022

--
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.

Jakob Borg

unread,
Apr 25, 2016, 4:31:24 PM4/25/16
to jonathan...@gmail.com, golang-nuts
2016-04-25 22:11 GMT+02:00 <jonathan...@gmail.com>:
> So what is the correct usage and expected behavior and what to do with
> b.RunParallel()?

While not an answer to what may cause the weirdness with
StopTimer()/StartTimer(), I've adopted the practice of simply running
ResetTimer() after initialization, before the benchmarking loop. I
haven't noticed bad data from benchmark runs.

//jb

jonathan...@gmail.com

unread,
Apr 25, 2016, 7:29:13 PM4/25/16
to golang-nuts
Same behavior with resettimer :(
Reply all
Reply to author
Forward
0 new messages