diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2020-02-06 16:12:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-06 16:12:54 +0100 |
commit | e24443f1bc7f10349c8ab6981364a883f0fc060a (patch) | |
tree | 8768247329d7dad3c28c82ed4e74bedf56bef82d | |
parent | 7ec7731f824b933cdd4f4a1f816c1f3e4862bef6 (diff) | |
download | Nim-e24443f1bc7f10349c8ab6981364a883f0fc060a.tar.gz |
make monotimes have zero overhead if you don't use it (#13338) [backport]
-rw-r--r-- | lib/std/monotimes.nim | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/lib/std/monotimes.nim b/lib/std/monotimes.nim index af08c576e..f819cbc8d 100644 --- a/lib/std/monotimes.nim +++ b/lib/std/monotimes.nim @@ -51,11 +51,6 @@ when defined(macosx): proc mach_timebase_info(info: var MachTimebaseInfoData) {.importc, header: "<mach/mach_time.h>".} - let machAbsoluteTimeFreq = block: - var freq: MachTimebaseInfoData - mach_timebase_info(freq) - freq - when defined(js): proc getJsTicks: float = ## Returns ticks in the unit seconds @@ -88,11 +83,6 @@ elif defined(windows): proc QueryPerformanceFrequency(res: var uint64) {. importc: "QueryPerformanceFrequency", stdcall, dynlib: "kernel32".} - let queryPerformanceCounterFreq = block: - var freq: uint64 - QueryPerformanceFrequency(freq) - 1_000_000_000'u64 div freq - proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} = ## Get the current `MonoTime` timestamp. ## @@ -105,6 +95,8 @@ proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} = result = MonoTime(ticks: (ticks * 1_000_000_000).int64) elif defined(macosx): let ticks = mach_absolute_time() + var machAbsoluteTimeFreq: MachTimebaseInfoData + mach_timebase_info(machAbsoluteTimeFreq) result = MonoTime(ticks: ticks * machAbsoluteTimeFreq.numer div machAbsoluteTimeFreq.denom) elif defined(posix): @@ -115,6 +107,10 @@ proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} = elif defined(windows): var ticks: uint64 QueryPerformanceCounter(ticks) + + var freq: uint64 + QueryPerformanceFrequency(freq) + let queryPerformanceCounterFreq = 1_000_000_000'u64 div freq result = MonoTime(ticks: (ticks * queryPerformanceCounterFreq).int64) proc ticks*(t: MonoTime): int64 = |