[JS + docs] improve std/monotimes module (#17103)
This commit is contained in:
parent
55a33e1664
commit
f07a072691
2 changed files with 49 additions and 43 deletions
|
|
@ -8,17 +8,21 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
##[
|
##[
|
||||||
The ``std/monotimes`` module implements monotonic timestamps. A monotonic
|
The `std/monotimes` module implements monotonic timestamps. A monotonic
|
||||||
timestamp represents the time that has passed since some system defined
|
timestamp represents the time that has passed since some system defined
|
||||||
point in time. The monotonic timestamps are guaranteed to always increase,
|
point in time. The monotonic timestamps are guaranteed to always increase,
|
||||||
meaning that that the following is guaranteed to work:
|
meaning that that the following is guaranteed to work:
|
||||||
|
]##
|
||||||
|
|
||||||
|
runnableExamples:
|
||||||
|
import std/os
|
||||||
|
|
||||||
.. code-block:: nim
|
|
||||||
let a = getMonoTime()
|
let a = getMonoTime()
|
||||||
# ... do some work
|
sleep(10)
|
||||||
let b = getMonoTime()
|
let b = getMonoTime()
|
||||||
assert a <= b
|
assert a < b
|
||||||
|
|
||||||
|
##[
|
||||||
This is not guaranteed for the `times.Time` type! This means that the
|
This is not guaranteed for the `times.Time` type! This means that the
|
||||||
`MonoTime` should be used when measuring durations of time with
|
`MonoTime` should be used when measuring durations of time with
|
||||||
high precision.
|
high precision.
|
||||||
|
|
@ -35,7 +39,7 @@
|
||||||
* `times module <times.html>`_
|
* `times module <times.html>`_
|
||||||
]##
|
]##
|
||||||
|
|
||||||
import times
|
import std/times
|
||||||
|
|
||||||
type
|
type
|
||||||
MonoTime* = object ## Represents a monotonic timestamp.
|
MonoTime* = object ## Represents a monotonic timestamp.
|
||||||
|
|
@ -53,18 +57,16 @@ when defined(macosx):
|
||||||
|
|
||||||
when defined(js):
|
when defined(js):
|
||||||
proc getJsTicks: float =
|
proc getJsTicks: float =
|
||||||
## Returns ticks in the unit seconds
|
## Returns ticks in the unit seconds.
|
||||||
|
when defined(nodejs):
|
||||||
{.emit: """
|
{.emit: """
|
||||||
var isNode = typeof module !== 'undefined' && module.exports
|
let process = require('process');
|
||||||
|
let time = process.hrtime();
|
||||||
if (isNode) {
|
`result` = time[0] + time[1] / 1000000000;
|
||||||
var process = require('process');
|
|
||||||
var time = process.hrtime()
|
|
||||||
return time[0] + time[1] / 1000000000;
|
|
||||||
} else {
|
|
||||||
return window.performance.now() / 1000;
|
|
||||||
}
|
|
||||||
""".}
|
""".}
|
||||||
|
else:
|
||||||
|
proc jsNow(): float {.importjs: "window.performance.now()".}
|
||||||
|
result = jsNow() / 1000
|
||||||
|
|
||||||
# Workaround for #6752.
|
# Workaround for #6752.
|
||||||
{.push overflowChecks: off.}
|
{.push overflowChecks: off.}
|
||||||
|
|
@ -75,7 +77,7 @@ when defined(js):
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
elif defined(posix) and not defined(osx):
|
elif defined(posix) and not defined(osx):
|
||||||
import posix
|
import std/posix
|
||||||
|
|
||||||
elif defined(windows):
|
elif defined(windows):
|
||||||
proc QueryPerformanceCounter(res: var uint64) {.
|
proc QueryPerformanceCounter(res: var uint64) {.
|
||||||
|
|
@ -84,11 +86,11 @@ elif defined(windows):
|
||||||
importc: "QueryPerformanceFrequency", stdcall, dynlib: "kernel32".}
|
importc: "QueryPerformanceFrequency", stdcall, dynlib: "kernel32".}
|
||||||
|
|
||||||
proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} =
|
proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} =
|
||||||
## Get the current `MonoTime` timestamp.
|
## Returns the current `MonoTime` timestamp.
|
||||||
##
|
##
|
||||||
## When compiled with the JS backend and executed in a browser,
|
## When compiled with the JS backend and executed in a browser,
|
||||||
## this proc calls `window.performance.now()`, which is not supported by
|
## this proc calls `window.performance.now()`.
|
||||||
## older browsers. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now)
|
## See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now)
|
||||||
## for more information.
|
## for more information.
|
||||||
when defined(js):
|
when defined(js):
|
||||||
let ticks = getJsTicks()
|
let ticks = getJsTicks()
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
import std/monotimes, times
|
discard """
|
||||||
|
targets: "c js"
|
||||||
|
"""
|
||||||
|
|
||||||
|
import std/[monotimes, times]
|
||||||
|
|
||||||
let d = initDuration(nanoseconds = 10)
|
let d = initDuration(nanoseconds = 10)
|
||||||
let t1 = getMonoTime()
|
let t1 = getMonoTime()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue