Don't know why but on my machine its faster:
goos: linux
goarch: amd64
pkg: playground/bench-time-now
BenchmarkTimeNow-8 30000000 49.3 ns/op
PASS
Showing top 10 nodes out of 16
flat flat% sum% cum cum%
1.02s 65.81% 65.81% 1.02s 65.81% runtime._ExternalCode
0.14s 9.03% 74.84% 0.14s 9.03% runtime.nanotime
0.12s 7.74% 82.58% 0.23s 14.84% time.now
0.11s 7.10% 89.68% 0.11s 7.10% runtime.walltime
0.11s 7.10% 96.77% 0.34s 21.94% time.Now
0.04s 2.58% 99.35% 0.38s 24.52%
playground/bench-time-now.BenchmarkTimeNow
0.01s 0.65% 100% 0.01s 0.65% runtime.scanblock
0 0% 100% 1.16s 74.84% runtime._System
0 0% 100% 0.01s 0.65% runtime.gcBgMarkWorker
0 0% 100% 0.01s 0.65% runtime.gcBgMarkWorker.func2
(pprof) list time.now
Total: 1.55s
ROUTINE ======================== playground/bench-time-now.BenchmarkTimeNow
in /home/lafolle/src/playground/bench-time-now/main_test.go
40ms 380ms (flat, cum) 24.52% of Total
. . 5: "testing"
. . 6: "time"
. . 7:)
. . 8:
. . 9:func BenchmarkTimeNow(b *testing.B) {
20ms 20ms 10: for i := 0; i < b.N; i++ {
20ms 360ms 11: time.Now()
. . 12: }
. . 13:}
ROUTINE ======================== time.now in
/usr/local/go/src/runtime/timestub.go
120ms 230ms (flat, cum) 14.84% of Total
. . 13:import _ "unsafe" // for go:linkname
. . 14:
. . 15:func walltime() (sec int64, nsec int32)
. . 16:
. . 17://go:linkname time_now time.now
20ms 20ms 18:func time_now() (sec int64, nsec int32, mono
int64) {
50ms 100ms 19: sec, nsec = walltime()
50ms 110ms 20: return sec, nsec, nanotime() - startNano
. . 21:}
(pprof)
On Thursday, 24 May 2018 13:38:36 UTC+5:30, [email protected] wrote:
>
> I been working on something performance intensive lately, one feature of
> it require me to calculate the time gap between operation which require
> time.Now to make time marks.
>
> After I benchmark time.Now on my computer, I found that the time.Now
> takes about 3758 ns to perform one operation, which is not very fast. The
> benchmark is here
> <https://gist.github.com/reinit/82608ab20e5aac3bd3c1eb5a8f78d23c>
> (benchmarked on my Ubuntu 18.04 LTS machine).
>
> So, I want to know is that normal to be this slow? If it is (slow), then
> does there is an alternative way to to that?
>
> Thank you!
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.