On Mon, May 26, 2008 at 3:59 PM, Edward A. Romana <erom at earthlink.net> wrote:
> Why is the execution time of Maxima for-loop statements different than the equivalent Fortran do-loop execution time.
In your first example, because you are running interpretively. In your
second example, because you haven't declared the type of your
variables and the Maxima compiler (like the Fortran compiler, I might
add) isn't clever enough to infer them.
> (%i23) for j:1 thru 1000 do (for k:1 thru 1000 do return);
>>> by the way "return" here is an uninterpreted symbol and
equivalent to "do true", say.
> Evaluation took 3.9800 seconds (3.9800 elapsed)...
> and for the the equivalent Fortran do-loop:
> Evaluation took 0.0800 seconds (0.0800 elapsed)
> the uncompiled Maxima loop is factor of 4.11/.08 or 51 times slower than Fortran.
Not at all surprising. Especially since a competent Fortran compiler
will optimize the do-nothing loops to zero executed instructions.
Anyway, most of the .08 seconds is probably process startup overhead.
> For the compiled equivalent Maxima for-loop function:...
> (%i35) f(1000);
> Evaluation took 0.8000 seconds (0.8000 elapsed)
> compiled Maxima is still a factor of 10 slower than fortran.
>
> One would think there is no need for symbolic mathematics in loop statements alone.
> where is the time factor of 10 consumed.
According to Fortran semantics, i and k are implicitly declared
integer. According to Maxima semantics, all variables are implicitly
declared "symbolic". Though in principle Maxima could infer that i
and k are integer-valued in this case, it doesn't (in theory, the user
could set i to anything in the body of the do, but that would rarely
be useful).
If you use modedeclare to declare them integer, you should see far
faster execution.
-s