Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timing an operation without being affected by clock changes

Tags:

time

go

I'm looking for:

1: startTime := time.Now()

2: // run something here that takes a while (measured in milliseconds)

3: duration := time.Since(startTime)

However, I need something that is immune to clock time changes. If the time is adjusted between lines 1 and 3, the duration will be inaccurate.

What are some common approaches for solving this problem, and what Go libraries could be relevant?

Thanks :)

like image 927
Pavel Avatar asked Oct 03 '22 14:10

Pavel


1 Answers

That lack of monotonic clock was detailed in issue 12914 (2015)

Since then, in August 2017 and Go 1.9, you now have a transparent Monotonic Time support:

The time package now transparently tracks monotonic time in each Time value, making computing durations between two Time values a safe operation in the presence of wall clock adjustments.
See the package docs and design document for details.

like image 91
VonC Avatar answered Oct 13 '22 11:10

VonC