HP Prime/Commands/Guides/Time Testing
Want to time-test HPPPL commands? Luckily, there's a simple way to do it, employing TEVAL.
The Time Testing Program
LOCAL n,m,v; f1() BEGIN // THE NIL FUNCTION END; f2() BEGIN // YOUR FUNCTION HERE END; loop1() BEGIN LOCAL c; FOR c FROM 1 TO n DO f1() END; END; loop2() BEGIN LOCAL c; FOR c FROM 1 TO n DO f2() END; END; EXPORT TIMETEST_EX(nn,mm,vv) BEGIN n := nn; v := vv; LOCAL a := {}; LOCAL c; FOR m FROM 1 TO mm DO LOCAL ms1 := TEVAL(loop1()); LOCAL ms2 := TEVAL(loop2()); LOCAL tstn := ms2/ms1; a[0] := tstn; END; CAS.median(a); END; EXPORT TIMETEST(vv) BEGIN RETURN TIMETEST_EX(10000,100,vv); END; EXPORT TIMETEST_SIMPLE() BEGIN RETURN TIMETEST(0); END;
Using The Program
Inside the body of function f2, write the command you wish to time-test. 'v' is an argument that you can feed into your program (see below).
TIMETEST_EX is the most flexible of the functions. It takes 3 arguments:
- The number of looped executions, 'n'
- The number of time-tests to average, 'm'
- The arbitrary value to pass in to f2, 'v'
TIMETEST is like TIMETEST_EX, but provides default values for 'n' (10000) and 'm' (100). It's good for smaller programs; you will likely want to adjust the values via TIMETEST_EX for larger programs.
TIMETEST_SIMPLE is like TIMETEST, but used when programs don't need a 'v' specified for it.
Results
It will run your code n*m times, finding the average running time versus the nil case. The result is produces is in TSTN (Times Slower Than Nil). This value is how many times slower it is than running an empty function. This value is portable between real and virtual calculators. In addition, if function A has a TSTN of X, and function B has a TSTN of Y, then A is (X-Y)/Y times faster/slower than B.
Be sure to run TIMETEST more than once. If the results differ a lot between runs, increase n or m and try again.
Limitations
TSTN, while supposed to be a consistent measurement between systems, may not be entirely equivalent between virtual and real calculators, as the architecture (and therefore the compiled code) differs between the ARM and x86/x64 computers. More research has to be done into this. For now, if you're looking for TSTN, use the virtual calculator; it completes tests faster.