Skip to content

Commit

Permalink
make monotimes have zero overhead if you don't use it (#13338) [backp…
Browse files Browse the repository at this point in the history
…ort]
  • Loading branch information
Araq authored Feb 6, 2020
1 parent 7ec7731 commit e24443f
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 e24443f

Please sign in to comment.