REBOL 3.0

Precision delta time measurements

Carl Sassenrath, CTO
REBOL Technologies
2-Nov-2009 18:23 GMT

Article #0289
Main page || Index || Prior Article [0288] || Next Article [0290] || 19 Comments || Send feedback

The delta-time function (or dt for short) is useful for timing your code. But, if you source it:

>> source delta-time
delta-time: make function! [[
   {Delta-time - return the time it takes to evaluate a block.}
   block [block!]
   /local start
][
   start: now/precise
   do block
   difference now/precise start
]]

you'll see that it uses now/precise.

A more accurate measurement would be to use stats/timer, which gives microsecond resolution.

The new function becomes:

delta-time: funct [
   {Delta-time - return the time it takes to evaluate a block.}
   block [block!]
][
   start: stats/timer
   do block
   to-time stats/timer - start / 1.0e6
]

Examples:

>> delta-time [loop 1000 [next "test"]]
== 0:00:00.000464
>> delta-time [wait 1]
== 0:00:01.001079

In A95, this change will be made, unless you know some reason not to.

See also the article: New Timebase.

19 Comments

REBOL 3.0
Updated 5-Nov-2024 - Edit - Copyright REBOL Technologies - REBOL.net