It would be helpful to give more information as to why you say "This 
doesn't work"? 
But, I'm guessing that you are not seeing a decline in times when using 
StartTimer/StopTimer.

It is likely that is because the copy function is fast, and frequent calls 
to StartTimer/StopTimer involve some error/overhead. So they are 
counteracting each other. Note that the documentation says the StopTimer 
can be used for "complex initialization", and I am guessing that a 1k copy 
does not count as complex. If you change the buffer size from 1024 to 
1024*16 then you will see that the StartTimer/StopTimer version is in fact 
faster. 

PeterGo's solution works fine, as does bench marking a larger buffer. But 
if your goal is to compare BenchmarkFilter3 and BenchmarkFilter4, then you 
could reasonably just ignore the the overhead, since it will be the same 
for both functions. 

On Wednesday, July 7, 2021 at 11:29:30 AM UTC-4 tapi...@gmail.com wrote:

> This doesn't work:
>
> func BenchmarkFilter3(b *testing.B) {
>     data := buildOrginalData()
>     data2 := make([]int, len(data))
>     b.ResetTimer()
>     for i := 0; i < b.N; i++ {
>         b.StopTimer()
>         copy(data2, data)
>         b.StartTimer()
>         _ = Filter3(data2)
>     }
> }
>
> On Wednesday, July 7, 2021 at 11:28:13 AM UTC-4 tapi...@gmail.com wrote:
>
>>
>> For example, I don't want the time consumed for "copy(data2, data)" being 
>> counted.
>> How to achieve this?
>>
>> func BenchmarkFilter3(b *testing.B) {
>>     data := buildOrginalData()
>>     data2 := make([]int, len(data))
>>     b.ResetTimer()
>>     for i := 0; i < b.N; i++ {
>>         copy(data2, data)
>>         _ = Filter3(data2)
>>     }
>> }
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c1f156d5-e582-4932-88f8-a2df6c987d85n%40googlegroups.com.

Reply via email to