Skip to content

Commit

Permalink
make monotimes have zero overhead if you don't use it (nim-lang#13338)…
Browse files Browse the repository at this point in the history
… [backport]

(cherry picked from commit e24443f)
  • Loading branch information
Araq authored and narimiran committed Feb 19, 2020
1 parent 6d65e69 commit 3a47e43
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/std/monotimes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
##
Expand All @@ -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):
Expand All @@ -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 =
Expand Down

0 comments on commit 3a47e43

Please sign in to comment.