Arjen Markus <
arjen.m...@gmail.com> writes:
> Here are some comments ...
>
> On Monday, April 15, 2019 at 11:59:07 AM UTC+2, Cecil Westerhof wrote:
>
>>
>> The code (comments and tips are welcome):
>> #!/usr/bin/env tclsh
>>
>>
>> proc displayTiming {} {
>> foreach {function description} {
>> withPow pow
>> withFloat float
>> withInt int
>> } {
>> set seconds [expr [lindex [time ${function}] 0] / 1000000.0]
>> puts [format "%-25s %.2E" "Timing with ${function}: " ${seconds}]
>> }
>> }
>>
>> proc withFloat {} {
>> set until [expr pow(10, $::exp)]
>> for {set i 0} {$i < ${until}} {incr i} {
>> }
>> }
>
> The value of until is calculated once, but in general it is better to brace your expressions:
>
> set until [expr {pow(10, $::exp)}]
Here it does not matter, but when used in the withPow proc it is a
speedup of almost 10.
>> proc withInt {} {
>> set until [expr int(pow(10, $::exp))]
>> for {set i 0} {$i < ${until}} {incr i} {
>> }
>> }
>
> pow() will evaluate its arguments as floats, AFAIK.
Yes, that is why I do an int call on the result. That gives almost 20%
performance boost.
> The problems here are:
> - You have not braced the expression, meaning that [expr] has to
> analyse the expression over and over again.
I knew that, but forgot. I almost never use expr.
> - You do not need an inner [expr] at all
> - Defining "exp" as a global variable, rather than getting it from
> the global namespace might reduce the run time further (by a very
> small amount that is)
For the other two it does not make a big difference, but when used
with withPow it almost halves the time.
>
> Try
>
> proc withPow {} {
> global exp
> for {set i 0} {$i < pow(10, $exp)} {incr i} {
> }
> }
Already done. Almost ten times faster.
> Or:
> proc withPow {} {
> global exp
> for {set i 0} {$i < 10**$exp} {incr i} {
This makes it almost three times as fast again. Takes only two times
the time as withFloat. That are quit significant savings. But still
withInt is significant faster.