Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Use CLOCK_MONOTONIC_COARSE in GetTickCount64 #2293

Merged
merged 1 commit into from
Dec 10, 2015

Conversation

stephentoub
Copy link
Member

Today, GetTickCount64 is implemented to use clock_gettime with CLOCK_MONOTONIC if it's available. This provides for high-resolution, however it trades off that precision for some efficiency.

This commit changes it to prefer CLOCK_MONOTONIC_COARSE if it's available (QueryPerformanceCounter still uses CLOCK_MONOTONIC). GetTickCount is typically used for coarse timings, e.g. used in a loop to determine whether more than 10 seconds have passed, and can have lower resolution in exchange for higher efficiency. For Windows, the MSDN docs explicitly state that it's likely to have no better than 10-15 millisecond resolution. Using CLOCK_MONOTONIC_COARSE instead of CLOCK_MONOTIC maps better to this notion, and improves Environment.TickCount throughput on my machine by ~20x. At the same time, on my machine it still provides well better than 10-15ms resolution, closer to ~4ms.

After this change, Environment.TickCount is still ~2x slower on my Linux machine than on my Windows machine (though with better precision), but my Linux machine is a VM, and I tested a bunch of different time mechanisms and I think this is the best we can do given the various constraints.

Fixes https://github.com/dotnet/coreclr/issues/2257
cc: @janvorli, @ianhays

Today, GetTickCount64 is implemented to use clock_gettime with CLOCK_MONOTONIC if it's available.  This provides for high-resolution, however it trades off that accuracy for some efficiency.

This commit changes it to prefer CLOCK_MONOTONIC_COARSE if it's available (QueryPerformanceCounter still uses CLOCK_MONOTONIC).  GetTickCount is typically used for coarse timings, e.g. used in a loop to determine whether more than 10 seconds have passed, and can have lower resolution in exchange for higher efficiency.  For Windows, the MSDN docs explicitly state that it's likely to have no better than 10-15 millisecond resolution. Using CLOCK_MONOTONIC_COARSE instead of CLOCK_MONOTIC maps better to this notion, and improves Environment.TickCount throughput on my machine by ~20x.  At the same time, on my machine it still provides well better than 10-15ms resolution, closer to ~4ms.
@janvorli
Copy link
Member

LGTM, thanks for looking into that!

@stephentoub
Copy link
Member Author

Thanks, Jan.

stephentoub added a commit that referenced this pull request Dec 10, 2015
Use CLOCK_MONOTONIC_COARSE in GetTickCount64
@stephentoub stephentoub merged commit 2aa39ea into dotnet:master Dec 10, 2015
@stephentoub stephentoub deleted the tickcount_perf branch December 10, 2015 19:19
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Use CLOCK_MONOTONIC_COARSE in GetTickCount64

Commit migrated from dotnet/coreclr@2aa39ea
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Environment.TickCount slower on Linux than Windows Core
3 participants