Simpler time difference function

Carl Sassenrath, CTO
REBOL Technologies
13-Aug-2009 5:31 GMT

Article #0233
Main page || Index || Prior Article [0232] || Next Article [0234] || 4 Comments || Send feedback

In A77 you can divide by time. I'm not sure why we didn't allow this earlier, but it's handy. Here's an example of why.

This is a handy piece of code for roughly measuring the time difference between two expressions:

diff-time: funct [reps [integer!] b1 [block!] b2 [block!]][
    t1: dt [loop reps b1]
    if zero? t1 [print "Use more repetitions." exit]
    t2: dt [loop reps b2]
    print [round/to t2 - t1 / t1 0.01% "slower"]

diff-time 1000000 [add 1.0 2.0] [add $1.0 $2.0]
13.13% slower

This code benefits from time division as well as the round to percent bug fix.

Now, I can use it for timing the speed of extension commands (natives):

secure [extension allow]
t: import %ext-test.dll

diff-time 1000000 [negate 1] [t-neg 1]
-6.84% slower
diff-time 1000000 [add 1 2] [t-addi 1 2]
-5.68% slower
diff-time 1000000 [add 1.0 2.0] [t-addd 1.0 2.0]
-10.61% slower

Ah ha! You have proof that extension commands are as fast (in fact a bit faster for some operations) than natives. Interesting, no? (The reason this happens is because the built-in actions like add must by polymorphic over so many datatypes, so extra conditional branching and dispatching occurs.)

Of course, keep in mind that results from this simple technique of measurement will vary due to multitasking activities of your system. Run your test a few times.


Updated 26-May-2024 - Edit - Copyright REBOL Technologies -