Update: improved test, as result of Mike's mail.
On my second hand 1.6 GHz PC, running XP note: 2048 is missing in the first, because testB gave an unreliable result (0 ms) I think that if getting time before & after a test case is within the timespan of the same 15 ms, times are equal. The µs = ms/#iterations. Mike was right, µs give more insight in the Pronto behaviour.
The test on the PC shows that the average time drops drastically when the number of iterations increase from 1024 to 4096 & 8192, then two small gains before it gets more or less stable from 32768 iterations.
The test on the Pronto shows that the average time drops drastically when the number of iteration increase from 32 to about 256 (testB), then it remains more or less stable with one exception in test B.
The test was simple: function testA() {var a=1000; a=a+a;a=a-a;} function testB() {var a=1000; a=a*a; a=a/a;}
As well on the PC as on the Pronto both tests yielded about the same result: speed difference between testA and TestB is lower than 1% which is not significant enough to tell that the +- test is faster than */-test on PC or Pronto. This can be different with other PC's or other instruction sets.
You find the improved program below the results. With this program you can compare two sets of instructions.
averages of 10 passes = 20886528 iterations #iterationstestAtestBresult(%) 102415ms 14.65µs16ms 15.63µs6.3A is faster 409632ms 7.81µs15ms 3.66µs53.1B is faster 819219ms 2.32µs18ms 2.20µs5.3B is faster 1638436ms 2.20µs31ms 1.89µs13.9B is faster 3276866ms 2.01µs62ms 1.89µs6.1B is faster 65536131ms 2.00µs127ms 1.94µs3.1B is faster 131072258ms 1.97µs255ms 1.95µs1.2B is faster 262144509ms 1.94µs508ms 1.94µs0.2B is faster 5242881010ms 1.93µs1010ms 1.93µs0both are equally fast 10485762023ms 1.93µs2020ms 1.93µs0.1B is faster
to process 20886528 iterations testAneeded 40546 ms, i.e. 1.94µs per iteration testBneeded 40315 ms, i.e. 1.94µs per iteration a difference of 0.23 seconds (0.57%) on a total of 40.55 seconds
On the Pronto
averages of 10 passes = 327598 iterations #iterationstestAtestBresult(%) 322ms 62.50µs2ms 62.50µs0both are equally fast 643ms 46.88µs4ms 62.50µs25.0A is faster 1287ms 54.69µs7ms 54.69µs0both are equally fast 25613ms 50.78µs14ms 54.69µs7.1A is faster 51226ms 50.78µs27ms 52.73µs3.7A is faster 102453ms 51.76µs53ms 51.56µs0both are equally fast 2048106ms 51.76µs113ms 55.18µs6.2A is faster 4096211ms 51.51µs212ms 51.76µs0.5A is faster 8192422ms 51.51µs423ms 51.64µs0.2A is faster 16384845ms 51.57µs845ms 51.57µs0both are equally fast
to process 327598 iterations testAneeded 16912 ms, i.e. 51.62µs per iteration testBneeded 17004 ms, i.e. 51.62µs per iteration a difference of 0.09 seconds (0.54%) on a total of 17.00 seconds
It would have taken 1084 seconds to run the 20886528 iterations on the Pronto
And here is the program:
function testA(){
}
function testB(){
}
// ------------------------ do not change the code below this line --------------------------
// HOW TO
// put code to be compared on performance inside testA & testB like shown
// if the code contains iterations, lower the values eMin and eMax to avoid very long wait times
// if the code contains heavy iterations consider putting eMin to 0 and eMax to 5 (for example)
// USE MODES
// dyadic use >> shows runtimes of code A versus code B, including the runtime of the iteration itself
// monadic use >> testB empty: shows runtime of code A including the iteration runtime, B shows iteration runtime
// niladic use >> testA & testB empty: should yield equal iteration runtimes; reality is different: read the remark
// REMARK
// note that any interference disturbs the test: run this without code and without touching mouse & keyboard
// there will be a difference in runtime even if the code is exactly the same
// now try this: enlarge the ProntoScript Console by dragging it with the mouse during a test
// you will notice a signicant difference in one of the tests
// EXAMPLE
// dyadic use: testA contained: var a=1000; a=a+a;a=a-a; testB contained: var a=1000; a=a*a; a=a/a;
//to process 2093056 iterations
//testAneeded 40640 ms
//testBneeded 40594 ms
//a difference of 0.05 seconds (0.12%) on a total of 41 seconds
//>>> proves that there is no difference in speed between +- and */
//
// monadic use: testA contained: var a=1000; a=a+a;a=a-a; testB was left empty
//to process 2093056 iterations
//testAneeded 40329 ms
//testBneeded 22344 ms
//a difference of 17.98 seconds (44.58%) on a total of 40.32 seconds
//>>> shows that the code in testA takes 18 seconds, while the iteration by itself required already 22 seconds
test();
function test() {
var d; // date
var a; // testA runtime in ms
var b; // testB runtime in ms
var i; // iterations, set by 2 to the power of e
var e; // exponent
var eMin; // exponent minimum
var eMax; // exponent maximum
var step; // =e-eMin
var n; // iteration counter
var r; // result in %, result=100*absolute(for-while)/while )
var c; // conditional text
var p; // pass number
var pMax; // maximum number of passes
var x; // total number of iterations
var aVector=[]; // contains results of all passes for testA
var bVector=[]; // contains results of all passes for testB
var pVector=[]; // contains number valid iterations per pass
var showEach=true; // set to true to get details of each pass, next to the average result
eMin=10; // lower values are not reliable
eMax=15; // higher values cause longer wait time (depending on processor speed)
pMax=10; // processing time is equivalent to p*sigma(2 to the power of e), where e = 12 up to 20 !!
// initiate vectors
n=0;
while (n<eMax-eMin) {
aVector[n]=0;
bVector[n]=0;
pVector[n]=pMax;
n++;}
p=0;
// watch the result in ProntoScript Console
while (p<pMax) {
System.print(showEach?("\n#iterations\ttestA\t\ttestB\t\tresult\tpass #"+p):("processing pass #"+p))
e=eMin;
while (e<eMax) {
i=Math.pow(2,e)
//test starts here
d=new Date().getTime();n=0;while(n<i){testA();n++;};a=new Date().getTime()-d;
d=new Date().getTime();n=0;while(n<i){testB();n++;};b=new Date().getTime()-d;
// test ends here
step=e-eMin;
if (a==0 | b==0 | a==NaN | b==NaN) {
pVector[step]--;}
else {
if (showEach) {
display(a,b,c,i,r)}
aVector[step]=aVector[step]+a;
bVector[step]=bVector[step]+b;
// System.print("aVector "+aVector+" bVector "+bVector);
}
e++;}
p++;}
x=0;
p=0;
while (p<eMax-eMin){
x=x+pVector[p]*Math.pow(2,eMin+p);
p++;}
System.print("\naverages of "+pMax+" passes = "+x+" iterations")
System.print("#iterations\ttestA\t\ttestB\t\tresult(%)")
e=eMin;
while (e<eMax) {
i=Math.pow(2,e);
step=e-eMin;
if (pVector[step]>0){
a=(aVector[step]/pVector[step]).toFixed(0);
b=(bVector[step]/pVector[step]).toFixed(0);
display(a,b,c,i,r);}
e++;}
e=eMin;
a=0;
b=0;
while (e<eMax) {
i=Math.pow(2,e);
step=e-eMin;
a=a+aVector[step];
b=b+bVector[step];
e++;}
System.print("\nto process "+x+" iterations");
System.print("testA\tneeded "+a+" ms, i.e. "+(1000*a/x).toFixed(2)+"µs per iteration");
System.print("testB\tneeded "+b+" ms, i.e. "+(1000*a/x).toFixed(2)+"µs per iteration");
r=((a>b)?a:b)/1000;
n=(Math.abs(a-b)/1000);
System.print("a difference of "+n.toFixed(2)+" seconds ("+(100*n/r).toFixed(2)+"%) on a total of "+r.toFixed(2)+" seconds");
System.print("\nend of test\n");
}
function display (a,b,c,i,r) {
// display results
if (a==0 | b==0 | a==NaN | b == NaN) {
r=""
c="result is not reliable"}
else {
if (a==b) {
r=0;
c="both are equally fast"}
else {
if (a<b) {
r=(100*Math.abs(a-b)/b).toFixed(1);
c="A is faster"}
else {
r=(100*Math.abs(b-a)/a).toFixed(1);
c="B is faster"}}}
System.print(i+"\t\t"+a+"ms "+(1000*a/i).toFixed(2)+"µs\t"+b+"ms "+(1000*b/i).toFixed(2)+"µs\t"+r+"\t"+c);
}
|