Benchmarking code that mutates its input data

82 views
Skip to first unread message

Orson Cart

unread,
Mar 18, 2020, 11:46:59 AM3/18/20
to golang-nuts

We have a slice containing a mix of data items, some of which are deemed to be 'valid', some 'invalid'. We need to remove the invalid items.

In the current implementation we have a function which takes the slice as input and removes the invalid' elements from it in place i.e. not from a copy.  So, after the function has completed, the slice will no longer contain any of the invalid elements.

We have alternative approaches to doing this but ideally we'd like to benchmark each. However we're struggling to set up the test data for this one because after the first call there will be no data to change - if the profiler were to call the function 1000 times, only the first call will have done any actual work. To make the benchmarked function do the same work each time we'd have to set up fresh data for each call to the function. We can do that but we don't want that setup to show in the timing for the benchmark.

Does anyone know of a solution to this?

TIA,
Orson

Sam Whited

unread,
Mar 18, 2020, 11:50:33 AM3/18/20
to golan...@googlegroups.com
I'd like to quickly suggest that the "filtering without allocating"
technique from this page is probably what you want and you may want to
consider if/why you even need to benchmark this:
https://github.com/golang/go/wiki/SliceTricks

However, if you do need to benchmark it:

On Wed, Mar 18, 2020, at 11:46, Orson Cart wrote:
> - if the profiler were to call the function 1000 times, only the first
> call will have done any actual work. To make the benchmarked function
> do the same work each time we'd have to set up fresh data for each
> call to the function. We can do that but we don't want that setup to
> show in the timing for the benchmark.
>
> Does anyone know of a solution to this?

Just call b.ResetTimer() after setting up your data: https://godoc.org/testing#B.ResetTimer

—Sam

--
Sam Whited

Robert Engels

unread,
Mar 18, 2020, 1:53:05 PM3/18/20
to Sam Whited, golan...@googlegroups.com
The test package has methods to disable the timing - wrap the setup in these.

> On Mar 18, 2020, at 10:50 AM, Sam Whited <s...@samwhited.com> wrote:
>
> I'd like to quickly suggest that the "filtering without allocating"
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6b8fcaa6-0c14-4e9c-8a22-71b9154dac7a%40www.fastmail.com.

Message has been deleted

Orson Cart

unread,
Mar 19, 2020, 12:11:52 PM3/19/20
to golang-nuts
On Wednesday, 18 March 2020 17:53:05 UTC, Robert Engels wrote:
The test package has methods to disable the timing - wrap the setup in these.

Thanks. I guess you're referring to StoptTimer, StartTimer and ResetTimer?

I think that in my case ResetTimer will do the trick. Unless I'm mistaken StartTimer and StopTimer are useful in more complex scenarios.

Orson Cart

unread,
Mar 19, 2020, 12:13:16 PM3/19/20
to golang-nuts
On Wednesday, 18 March 2020 15:50:33 UTC, Sam Whited wrote:
Just call b.ResetTimer() after setting up your data: https://godoc.org/testing#B.ResetTimer

Thanks, I'll take a look
 
Reply all
Reply to author
Forward
0 new messages