-j
goos: linuxgoarch: amd64pkg: playground/bench-time-nowBenchmarkTimeNow-8 30000000 49.3 ns/opPASS
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.nowTotal: 1.55sROUTINE ======================== 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)
--
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.
For more options, visit https://groups.google.com/d/optout.
Are you using the latest version of Go? time.Now() has been optimized recently.
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 (benchmarked on my Ubuntu 18.04 LTS machine).
1825 ns/op. It's still slow though (maybe related to problems on my OS/hardware), but it way faster than the one under Go 1.10.
I did the test on both my computer and Cloud 9 IDE, and able reproduce the result.
After dig into the code and I found they've changed the underlaying calls, maybe that's why newer version of time.Now is slower.
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00 0.000418 4 100 gettimeofday
------ ----------- ----------- --------- --------- ----------------
100.00 0.000418 100 total