Since the subroutine had a wrong variable name in it, I did it again and got slightly better results. First, the corrected code:
sub countdown cnt:
if #cnt:
return sub countdown dec #cnt;
else:
return #cnt;
endif;
endsub;
stopwatch reset;
gosub countdown 1000;
stopwatch;
Then the result:
Elapsed time: 0.50052809715271 seconds
Trying again with var instead of #:
sub countdown cnt:
if var cnt:
return sub countdown dec var cnt;
else:
return var cnt;
endif;
endsub;
stopwatch reset;
gosub countdown 1000;
stopwatch;
Elapsed time: 0.51695919036865 seconds
This slowed it down slightly. The curious thing is that #cnt calls evalvar() first, and this then calls getuservar(), whereas var cnt calls getuservar() directly. Based on this, I was expecting it to be faster. Factors that could explain the opposite result are #cnt is handled while parsing the line, while var cnt is handled by evaluating an expression, and var cnt is a longer string than #cnt, which means reading the line takes longer.
Trying var cnt with a function:
def countdown fn countdown dec var a onlyif var a =a;
stopwatch reset;
print fn countdown 10000;
stopwatch;
Elapsed time: 0.20279908180237 seconds
This also slowed things down, and it was by a lot more compared to the earlier result, which was 0.14718699455261. In the function for evaluating polish notation expressions, evaluation of prepended variables is handled before operators, but the extra line parsing shouldn't make as much of a difference, because the function definition is executed only once.
Since the subroutine had a wrong variable name in it, I did it again and got slightly better results. First, the corrected code:
Then the result:
Trying again with var instead of #:
This slowed it down slightly. The curious thing is that
#cnt
calls evalvar() first, and this then calls getuservar(), whereasvar cnt
calls getuservar() directly. Based on this, I was expecting it to be faster. Factors that could explain the opposite result are#cnt
is handled while parsing the line, whilevar cnt
is handled by evaluating an expression, andvar cnt
is a longer string than#cnt
, which means reading the line takes longer.Trying
var cnt
with a function:This also slowed things down, and it was by a lot more compared to the earlier result, which was 0.14718699455261. In the function for evaluating polish notation expressions, evaluation of prepended variables is handled before operators, but the extra line parsing shouldn't make as much of a difference, because the function definition is executed only once.