Skip to content

Instantly share code, notes, and snippets.

@timotheecour
Last active August 26, 2021 01:10
Show Gist options
  • Save timotheecour/e5d85be09b141b4bf9f877e1c8731025 to your computer and use it in GitHub Desktop.
Save timotheecour/e5d85be09b141b4bf9f877e1c8731025 to your computer and use it in GitHub Desktop.
when defined case1: # D20210825T174515:here gitissue
import std/times
import std/monotimes
import std/cputicks
proc mach_absolute_time(): int64 {.importc, header: "<mach/mach.h>".}
template mainAux(algo)=
block:
let t = cpuTime()
var c = default(typeof(algo))
let n = 10_000_000
for i in 0..<n:
c += algo
let t2 = cpuTime()
echo (astToStr(algo), t2 - t, c)
proc main =
mainAux(getCpuTicks())
mainAux(cpuTime())
mainAux(getMonoTime().ticks)
mainAux(mach_absolute_time())
main()
when defined case2:
import std/times
import std/monotimes
import std/cputicks
template mainAux(algo)=
block:
let t = cpuTime()
var c = default(typeof(algo))
let n = 10_000_000
var count = 0
for i in 0..<n:
let a1 = algo
let a2 = algo
c += a2 - a1
if a2 > a1: count.inc
let t2 = cpuTime()
echo (astToStr(algo), t2 - t, c, count / n)
proc main =
mainAux(getCpuTicks())
mainAux(cpuTime())
mainAux(getMonoTime().ticks)
main()
when defined case3: # see D20210825T174515
import std/times
import std/monotimes
# import std/cputicks
when defined(osx):
proc mach_absolute_time(): int64 {.importc, header: "<mach/mach.h>".}
else:
type
Clockid {.importc: "clockid_t", header: "<time.h>", final.} = object
TimeSpec {.importc: "struct timespec", header: "<time.h>",
final, pure.} = object ## struct timespec
tv_sec: int ## Seconds.
tv_nsec: int ## Nanoseconds.
# var CLOCK_REALTIME {.importc: "CLOCK_REALTIME", header: "<time.h>".}: Clockid
# var CLOCK_MONOTONIC {.importc: "CLOCK_MONOTONIC", header: "<time.h>".}: Clockid
var CLOCK_REALTIME {.importc: "CLOCK_REALTIME", header: "<time.h>".}: Clockid
var CLOCK_MONOTONIC_RAW {.importc: "CLOCK_MONOTONIC_RAW", header: "<time.h>".}: Clockid
proc clock_gettime(clkId: Clockid, tp: var Timespec): cint {.importc: "clock_gettime", header: "<time.h>".}
proc getTicks2(): int64 =
when defined(osx): mach_absolute_time()
else:
var ts: Timespec
# discard clock_gettime(CLOCK_MONOTONIC, ts)
# discard clock_gettime(CLOCK_MONOTONIC, ts)
# discard clock_gettime(CLOCK_REALTIME, ts)
discard clock_gettime(CLOCK_MONOTONIC_RAW, ts)
result = ts.tv_sec.int64 * 1_000_000_000 + ts.tv_nsec.int64
const header =
when defined(posix): "<x86intrin.h>"
else: "<intrin.h>"
proc getCpuTicksImpl(): uint64 {.importc: "__rdtsc", header: header.}
template getCpuTicks*(): int64 =
cast[int64](getCpuTicksImpl())
template mainAux(algo)=
block:
let t = cpuTime()
var c = default(typeof(algo))
# let n = 10_000_000
let n = 100_000
for i in 0..<n:
c += algo
let t2 = cpuTime()
echo (astToStr(algo), t2 - t, c)
proc main =
mainAux(getCpuTicks())
mainAux(cpuTime())
mainAux(getMonoTime().ticks)
mainAux(getTicks2())
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment