Difference between revisions of "TI-BASIC:Better Code Timings"

From Learn @ Cemetech
Jump to navigationJump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
= Instructions =
 
= Instructions =
  
First, write the name and model of your calculator in the table below, then use this code to measure your loop overhead (without an empty line).
+
First, write the name and model of your calculator in the table below, then use this code to measure your loop overhead (with an empty line).
  
Add the number of iterations you used to the "iterations" column, and [time taken]/[iterations] to the "average" column. Then subtract your loop overhead from the result and list it in the "time" column.
+
Add the number of iterations you used to the "iterations" column, and the calculated time taken to the "average" column. Then subtract your loop overhead from the result and list it in the "time" column.
  
 
  FnOff
 
  FnOff
Line 12: Line 12:
 
  0→Ymax
 
  0→Ymax
 
  DispGraph //if it is a graphing command
 
  DispGraph //if it is a graphing command
  startTmr→θ
+
  1000->I%    //iterations
  Repeat checkTmr(θ
+
  startTmr->Tmin
 +
While startTmr=Tmin:End
 +
For([recursiven],1,I%
 +
1
 
  End
 
  End
  For(𝑛1,[iterations])    //sequence variable 𝑛; note the closing parenthesis.
+
startTmr->Tmax
  //test code here
+
  For([recursiven],1,|E9:
 +
If startTmr=Tmax
 
  End
 
  End
  checkTmr(θ+1
+
  Disp (Tmax-Tmin-1+round(1-[recursiven]/99,2)-0.63)/I%*1000
 +
 
 +
Alternatively, use the following timing code to include storing to Ans (":1") in the overhead:
 +
 
 +
1000->I%
 +
startTmr->Tmin
 +
While startTmr=Tmin:End
 +
For([recursiven],1,I%
 +
[code]
 +
End
 +
startTmr->Tmax
 +
For([recursiven],1,|E9:
 +
If startTmr=Tmax
 +
End
 +
Disp (Tmax-Tmin-1+round(1-[recursiven]/99,2)-1.74)/I%*1000
 +
 
 +
In either case, you must *calibrate these loop overheads to your own calculator's speed*.
  
 
__TOC__
 
__TOC__
 +
 +
==Variable Access==
 +
 +
All variables are equal to 1. The following timings include storing to Ans as overhead. Code was executed immediately after RAM clear.
 +
 +
{| class="wikitable"
 +
! Variable || Iters || Time (ms) || Username
 +
|-
 +
| Constant 1 || 1000 || 0 (by def) || 2.55MP jsTIfied / lirtosiast
 +
|-
 +
| Constant pi || 1000 || -.02 || "
 +
|-
 +
| Constant i || 1000 || -.08 || "
 +
|-
 +
| All system vars || 1000 || 0.11
 +
|-
 +
| X (L1~L6 deleted) || 1000 || 0.47
 +
|-
 +
| X (L1-L6 not deleted) || 1000 || 0.48
 +
|-
 +
| X (after 26 other real vars created) || 1000 || 0.79
 +
|-
 +
| Z (after 26 other real vars created) || 1000 || 0.79
 +
|}
 +
 +
The following timings do not include storing to Ans as overhead.
 +
 +
{| class="wikitable"
 +
! Variable || Value || Iters || Time (ms) || Username
 +
|-
 +
| Str1 || "A" || 1000 || 1.95 || "
 +
|-
 +
| Str1 || 100 "A"s || 1000 || 2.09
 +
|}
  
 
== Math Commands ==
 
== Math Commands ==
Line 114: Line 168:
 
{| class="wikitable"
 
{| class="wikitable"
 
! Command !! Iterations !! Average (ms) !! Overhead (ms) !! Time (ms)!! Username  
 
! Command !! Iterations !! Average (ms) !! Overhead (ms) !! Time (ms)!! Username  
 +
|-
 +
| [A]  || 10000 || 8.5 || 0.6 || 7.9 || 2.55MP/lirtosiast
 
|-
 
|-
 
| [A]^^T //20x2 || 10000 || 9.3 || 0.6 || 8.7 || 2.55MP/lirtosiast
 
| [A]^^T //20x2 || 10000 || 9.3 || 0.6 || 8.7 || 2.55MP/lirtosiast
Line 119: Line 175:
 
| [A]^^T //2x20 || 10000 || 9.4 || 0.6 || 8.8 || 2.55MP/lirtosiast
 
| [A]^^T //2x20 || 10000 || 9.4 || 0.6 || 8.8 || 2.55MP/lirtosiast
 
|-
 
|-
| [A]^^T //20x20 || 1000 || 62 || 0.6 || ~62 || 2.55MP/lirtosiast
+
| [A]^^T || 1000 || 62 || 0.6 || ~62 || 2.55MP/lirtosiast
 +
|-
 +
| rowSwap([A],1,2 || 10000 || 17 || 0.6 || ~17 || 2.55MP/lirtosiast
 +
|-
 +
| rowSwap([A],1,20 || 10000 || 17 || 0.6 || ~17 || 2.55MP/lirtosiast
 +
|-
 +
|}
 +
 
 +
It is clear that matrix multiplication should not be used to re-order the rows or columns of a matrix when speed is a priority. Use rowSwap( instead: ten rowSwap( take 0.17 seconds while one multiplication takes 11.8 seconds.
 +
 
 +
{| class="wikitable"
 +
! Command !! Iterations !! Average (ms) !! Overhead (ms) !! Time (ms)!! Username
 +
|-
 +
| [A][B]  //B=zero matrix || 10 || 13,000 || 0.6 || 13,000 || 2.55MP/lirtosiast
 +
|-
 +
| [A][B]  //B=identity(20 || 10 || 11,800 || 0.6 || 11,800 || 2.55MP/lirtosiast
 +
|-
 +
| [A][B]  //B=exchange matrix || 10 || 11,800 || 0.6 || 11,800 || 2.55MP/lirtosiast
 
|}
 
|}
  
Line 199: Line 272:
 
|-
 
|-
 
| For(''n'',1,100000 || .69  || kgmstwo  
 
| For(''n'',1,100000 || .69  || kgmstwo  
 +
|-
 +
| incl. ":1" || 1.74 || 2.55MP jsTIfied / lirtosiast
 
|}
 
|}
  

Latest revision as of 17:45, 3 May 2016

Instructions

First, write the name and model of your calculator in the table below, then use this code to measure your loop overhead (with an empty line).

Add the number of iterations you used to the "iterations" column, and the calculated time taken to the "average" column. Then subtract your loop overhead from the result and list it in the "time" column.

FnOff
PlotsOff
0→Xmin
94→Xmax
-62→Ymin
0→Ymax
DispGraph //if it is a graphing command
1000->I%    //iterations
startTmr->Tmin
While startTmr=Tmin:End
For([recursiven],1,I%
1
End
startTmr->Tmax
For([recursiven],1,|E9:
If startTmr=Tmax
End
Disp (Tmax-Tmin-1+round(1-[recursiven]/99,2)-0.63)/I%*1000

Alternatively, use the following timing code to include storing to Ans (":1") in the overhead:

1000->I%
startTmr->Tmin
While startTmr=Tmin:End
For([recursiven],1,I%
[code]
End
startTmr->Tmax
For([recursiven],1,|E9:
If startTmr=Tmax
End
Disp (Tmax-Tmin-1+round(1-[recursiven]/99,2)-1.74)/I%*1000

In either case, you must *calibrate these loop overheads to your own calculator's speed*.

Variable Access

All variables are equal to 1. The following timings include storing to Ans as overhead. Code was executed immediately after RAM clear.

Variable Iters Time (ms) Username
Constant 1 1000 0 (by def) 2.55MP jsTIfied / lirtosiast
Constant pi 1000 -.02 "
Constant i 1000 -.08 "
All system vars 1000 0.11
X (L1~L6 deleted) 1000 0.47
X (L1-L6 not deleted) 1000 0.48
X (after 26 other real vars created) 1000 0.79
Z (after 26 other real vars created) 1000 0.79

The following timings do not include storing to Ans as overhead.

Variable Value Iters Time (ms) Username
Str1 "A" 1000 1.95 "
Str1 100 "A"s 1000 2.09

Math Commands

Unless specified, all variables are equal to 1.

Command Iterations Average (ms) Loop overhead (ms) Time (ms) Username
N=I% and FV=PV 1000 4.? 0.6 ~4 2.55MP/lirtosiast
N+FV[i]=I%+PV[i] 1000 7.? 0.6 ~7 2.55MP/lirtosiast

List Commands

Unless specified, all list/matrix elements are equal to 1, and all dimensions are equal to 20.

Command Iterations Average (ms) Overhead (ms) Time (ms) Username
L1 1000 2.? 0.6 ~2 2.55MP/lirtosiast
not(L1 1000 9.? 0.6 ~9 2.55MP/lirtosiast
L1+0 1000 20 0.6 ~20 2.55MP/lirtosiast
cumSum(L1 1000 19 0.6 ~19 2.55MP/lirtosiast
DeltaList(L1 1000 26 0.6 ~26 2.55MP/lirtosiast
augment(L1,L1 10000 9.4 0.6 8.8 2.55MP/lirtosiast
augment(L1,{1 10000 7.7 0.6 7.1 2.55MP/lirtosiast
augment({1},L1 //dim=20 10000 7.7 0.6 7.1 2.55MP/lirtosiast
augment({1},{1 10000 6.0 0.6 5.4 2.55MP/lirtosiast
DeltaList(L1 //dim=20 1000 26 0.6 ~26 2.55MP/lirtosiast
Fill(1,L1 //dim=20 10000 3.9 0.6 3.3 2.55MP/lirtosiast

Below we can see that using the same variable for the seq( variable as the For( variable doesn't slow anything down. The value of the variable before seq( was called is restored in a constant amount of time.

Command Iterations Average (ms) Overhead (ms) Time (ms) Username
seq(1,n,1,20 //loop var n 1000 70 0.6 70 2.55MP/lirtosiast
seq(1,n,1,20 //loop var X 1000 71 1.1 70 2.55MP/lirtosiast
seq(1,X,1,20 //loop var n 1000 82 0.6 82 2.55MP/lirtosiast
seq(1,X,1,20 //loop var X 1000 83 1.1 82 2.55MP/lirtosiast

seq( has a large overhead (about 3.5 ms per element), but the penalty doesn't increase by much when the expression inside the seq( is large. Vectorized list operations outside the seq( are only marginally faster than operations inside the seq(.

Command Iterations Average (ms) Overhead (ms) Time (ms) Username
seq(1+1,n,1,20 1000 92 0.6 92 2.55MP/lirtosiast
1+seq(1,n,1,20 1000 92 0.6 92 2.55MP/lirtosiast
seq(not(1),n,1,20 1000 79 0.6 79 2.55MP/lirtosiast
not(seq(1,n,1,20 1000 76 0.6 76 2.55MP/lirtosiast

binomcdf(N-1,0 is often used to construct a list of a certain length N, but binompdf( can be slightly faster. The difference is about equal to the time for a call to cumSum(. When called with a second argument that is not 0 or 1, binompdf( slows down dramatically, by a factor of 60.

Command Iterations Average (ms) Overhead (ms) Time (ms) Username
binompdf(19,0 1000 5 0.6 5 2.55MP/lirtosiast
binomcdf(19,0 1000 20 0.6 20 2.55MP/lirtosiast
1 or binompdf(19,0 1000 16 0.6 16 2.55MP/lirtosiast
binompdf(19,1 1000 5 0.6 5 2.55MP/lirtosiast
binompdf(19,.5 1000 296 0.6 296 2.55MP/lirtosiast

Matrix Commands

When using matrix commands, remember that matrix multiplication is O(n^3). It's probably slower than other solutions for large matrices.

Command Iterations Average (ms) Overhead (ms) Time (ms) Username
[A] 10000 8.5 0.6 7.9 2.55MP/lirtosiast
[A]^^T //20x2 10000 9.3 0.6 8.7 2.55MP/lirtosiast
[A]^^T //2x20 10000 9.4 0.6 8.8 2.55MP/lirtosiast
[A]^^T 1000 62 0.6 ~62 2.55MP/lirtosiast
rowSwap([A],1,2 10000 17 0.6 ~17 2.55MP/lirtosiast
rowSwap([A],1,20 10000 17 0.6 ~17 2.55MP/lirtosiast

It is clear that matrix multiplication should not be used to re-order the rows or columns of a matrix when speed is a priority. Use rowSwap( instead: ten rowSwap( take 0.17 seconds while one multiplication takes 11.8 seconds.

Command Iterations Average (ms) Overhead (ms) Time (ms) Username
[A][B] //B=zero matrix 10 13,000 0.6 13,000 2.55MP/lirtosiast
[A][B] //B=identity(20 10 11,800 0.6 11,800 2.55MP/lirtosiast
[A][B] //B=exchange matrix 10 11,800 0.6 11,800 2.55MP/lirtosiast

Home Screen Commands

Command Iterations Average (ms) Loop overhead (ms) Time (ms) Username
ClrHome 1000 29 1.2 28 kgmstwo
Disp " 1000 51 1.2 50 kgmstwo
Output(1,1," 10000 3.8 1.2 2.6 kgmstwo
Output(1,1,"A 10000 4.4 1.2 3.2 kgmstwo
Output(1,1,"AB 10000 5.0 1.2 3.8 kgmstwo
Output(1,1,"ABC 10000 5.6 1.2 4.4 kgmstwo
ClrHome 1000 27 .68 26 kgmstwo

Graphics Commands

Command Iterations Average (ms) Loop overhead (ms) Time (ms) Username
Pxl-On(0,0 10000 2.9 1.2 1.7 kgmstwo
Pxl-Off(0,0 10000 2.9 1.2 1.7 kgmstwo
Pxl-Change(0,0 10000 2.9 1.2 1.7 kgmstwo
Horizontal -5 1000 36 1.2 35 kgmstwo
Horizontal -5 1000 35 .68 34 kgmstwo
Vertical 5 1000 25 1.2 24 kgmstwo
ClrDraw 1000 101 1.2 100 kgmstwo
StoPic Pic1 10000 4.7 1.2 3.5 kgmstwo
StoPic Pic1 10000 4.2 .68 3.5 kgmstwo
RclPic Pic1 1000 28 1.2 27 kgmstwo
Pt-On(10,-10 10000 4.1 1.2 2.9 kgmstwo
Pt-On(10,-10 10000 4.5 1.2 3.3 kgmstwo
Pt-On(10,-10 10000 4.0 .68 3.3 kgmstwo
:ZStandard:84→Xmin:72→Ymax:ZInteger 100 220 .68 220 kgmstwo
0->Xmin:1->DeltaX:0->Ymin:1->DeltaY 1000 14 .68 13 kgmstwo
0->Xmin:94->Xmax:-63->Ymin:0->Ymax 1000 18 .68 17 kgmstwo
Line(5,0,5,-62 1000 27 .68 26 kgmstwo
Line(5,0,5,-62,0 1000 27 .68 26 kgmstwo
Line(0,-5,94,-5 1000 37 .68 36 kgmstwo
Line(0,-5,94,-5,0 1000 37 .68 36 kgmstwo
Line(10,-10,10,-10 10000 7.2 .68 6.5 kgmstwo
Line(10,-10,10,-10,0 10000 7.4 .68 6.7 kgmstwo

note: multiple commands always on their own line.

Loop Overheads

Command Average (ms) Username
For(n,1,100000) .83 kgmstwo
For(n,1,100000 .69 kgmstwo
incl. ":1" 1.74 2.55MP jsTIfied / lirtosiast

Calculators Used

Username Model OS version
kgmstwo 84+SE 2.43
lirtosiast 84+ 2.55MP