Hi folks,
I was testing some code and I noticed something that struck me as little odd. In the following code:
we have routines Set1, Set2, and Set3. They do the same thing, but change where time.Now().Unix() is called.
Set1 calls for the time before it acquires a mutex, eventually passing that time onto a 3rd function
Set2 calls for the time after it's acquired the lock, and passes the value onto the 3rd function.
Set3 calls for the time after it's acquired the lock as part of its call to the 3rd function.
The benchmarks for these operations is different:
$ go test -bench .
goos: linux
goarch: amd64
BenchmarkStateSet1-64 1000000 1156 ns/op
BenchmarkStateSet2-64 1295552 833 ns/op
BenchmarkStateSet3-64 2348185 703 ns/op
PASS
ok _/home/jimr/tmp/odd 5.739s
$ go test -bench .
goos: linux
goarch: amd64
BenchmarkStateSet1-64 1000000 1146 ns/op
BenchmarkStateSet2-64 3256602 603 ns/op
BenchmarkStateSet3-64 1439412 842 ns/op
PASS
ok _/home/jimr/tmp/odd 5.983s
$ go test -bench .
goos: linux
goarch: amd64
BenchmarkStateSet1-64 1000000 1130 ns/op
BenchmarkStateSet2-64 1772056 772 ns/op
BenchmarkStateSet3-64 2637054 433 ns/op
PASS
ok _/home/jimr/tmp/odd 6.325s
Based on the difference between Set2/Set3 I'm wondering if this is just a case of the numbers being small enough that it's hard to reliabily measure them, but I'm struck how much slower Set1 is than Set2/Set3, apparently due to whether or not the time.Now().Unix() call is made within the lock? Why would that be?
Jim