Re: [go-nuts] The smaller argument a time.Sleep call takes, the more allocations?

2019-04-20 Thread andrey mirtchovski
You need the -benchmem flag to get a report of allocations:

$ go test -bench=. -benchmem
goos: darwin
goarch: amd64
BenchmarkTestSleep_2000-4   1 2005447537 ns/op  456 B/op
 3 allocs/op
BenchmarkTestSleep_1000-4   1 1001627153 ns/op   64 B/op
 1 allocs/op
BenchmarkTestSleep_500-4   2 500908757 ns/op   36 B/op
   1 allocs/op
BenchmarkTestSleep_100-4  10 101720516 ns/op6 B/op
   0 allocs/op
BenchmarkTestSleep_10-4   100   12494871 ns/op0 B/op
 0 allocs/op
PASS
ok  _/tmp 6.911s

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] The smaller argument a time.Sleep call takes, the more allocations?

2019-04-20 Thread T L
The following benchmark is modified from 
https://github.com/golang/go/issues/30802

package main

import (
"testing"
"time"
)

func SleepTest(milliseconds time.Duration) {
time.Sleep(time.Millisecond * milliseconds)
}

func BenchmarkTestSleep_2000(b *testing.B) {
for i := 0; i < b.N; i++ {
SleepTest(2000)
}
}

func BenchmarkTestSleep_1000(b *testing.B) {
for i := 0; i < b.N; i++ {
SleepTest(1000)
}
}

func BenchmarkTestSleep_500(b *testing.B) {
for i := 0; i < b.N; i++ {
SleepTest(500)
}
}

func BenchmarkTestSleep_100(b *testing.B) {
for i := 0; i < b.N; i++ {
SleepTest(100)
}
}

func BenchmarkTestSleep_10(b *testing.B) {
for i := 0; i < b.N; i++ {
SleepTest(10)
}
}


The result:
$ go test -bench=.
goos: linux
goarch: amd64
pkg: app/t
BenchmarkTestSleep_2000-4  12000142946 ns/op
BenchmarkTestSleep_1000-4  11000192805 ns/op
BenchmarkTestSleep_500-4   2 500191727 ns/op
BenchmarkTestSleep_100-4  10 100179544 ns/op
BenchmarkTestSleep_10-4  100  10164319 ns/op


It is some strange that when the argument is less than 1s, the smaller it 
is, the more allocations.
Bu, or my understanding is wrong?

-- 
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.
For more options, visit https://groups.google.com/d/optout.