octave, matlab, maxima



There are several aspects to this comparison of do loops in maxima and
fortran.
One is that fortran is not as general as lisp or maxima, and therefore the
fortran
compiler does not need to worry about some issues, and so can generate good
code.

as an example showing the generality of maxima:

assume(x>0);
for i:0 thru 10*x step x do print(i) ;  ==> prints 0, x, 2x, ...10x.

If you don't assume x>0, it refuses on the grounds that 0>10x is not known!

this also works in maxima:
for i:10^1000 thru 10^1000+4 do print("hello");

whether either of these shows it is "useful" or not is up to you.

The next issue is that maxima language, declared, then translated to lisp
and then compiled to binary code
goes through the lisp compiler.  
Some lisp compilers are very clever, and might notice, for example, that the
interior of the loop did
nothing and could just be eliminated. SBCL is fairly clever and might
notice. Sometimes Maxima is built
on top of SBCL. Some lisp compilers are very un-clever, including others
that can also support Maxima.

Then there is the model of numbers implemented in Lisp.  There is one
obvious way to implement numbers in fortran,
but because a number in Lisp can be of any length, there are other issues
involved that do not come up in fortran. 
 Even if a lisp variable is declared as being used only for a fixnum and
therefore more like a fortran number, there are still implementation issues
that may make arithmetic somewhat slower, in some lisp implementations, than
comparable computations in fortran. For example, just because x is a fixnum
does not mean that 10*x is a fixnum. Fortran would just overflow. Lisp would
return 10*x, though some compilers might generate code that would overflow
just like fortran, depending on "optimization level".

The real response to your issue is probably this:

If you are truly doing numeric computation that can be done entirely in
fortran, but choose to write the program in Maxima, you will probably find
that it runs considerably slower than in fortran (or C).  Some effort can be
expended to make the Maxima program run faster, and maybe even faster than
fortran, depending on the implementations of lisp and fortran, as well as
the expertise brought to bear.  Most people are willing to take a hit of a
factor of 2 or 3 slower than good fortran.

On the other hand, if your code is substantially symbolic, it is not a big
deal to have a "slow" do loop index operating on something like
  for i:1 thru 100 do   <some big hairy symbolic computation that you can't
even express in fortran> ...


RJF







Another is that 

> -----Original Message-----
> From: maxima-bounces at math.utexas.edu 
> [mailto:maxima-bounces at math.utexas.edu] On Behalf Of Edward A. Romana
> Sent: Tuesday, May 27, 2008 12:19 PM
> To: Stavros Macrakis
> Cc: maxima mailing list
> Subject: Re: [Maxima] octave, matlab, maxima
> 
> ~~~
> 
> Ok, flowing your advice, I compared the execution time of 2 
> Maxima and Fortran nested 
> do-nothing loops compiled with mode_declare([j,k], fixnum) 
> and implicit none respectively. 
> Also I increased the number of loops to overcome process 
> startup time overheads. 
> And in the Fortran pgm I added a simple integer counter m=m+1 
> instruction to to overcome 
> the Fortran compiler optimization of the do-nothing loops to 
> zero executed instructions.
> 
> The result is that Maxima do-nothing loops compiled using 
> mode_declare are 20 times 
> faster than Maxima compilation without it. However Fortran is 
> still 196 times faster 
> than Maxima compiled with mode_declare([j,k], fixnum) indexes . 
> 
> If I understand your explanation, in Maxima even with the 
> loop index declared fixnum, 
> the Maxima index variable remains symbolic that is a symbolic 
> integer expression in the 
> body of the for-loop can redefine the index. This does not 
> seem too useful me, 
> but I am willing to be convinced otherwise, namely are there 
> algorithm examples 
> where the expensive symbolic for-loop index is necessary or useful?
>  
> 
> Ed
> 
> ~~~
> 
> At 03:55 PM 5/26/2008, Stavros Macrakis wrote:
> >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.
> ><...>
> >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
> >_______________________________________________
> >Maxima mailing list
> >Maxima at math.utexas.edu
> >http://www.math.utexas.edu/mailman/listinfo/maxima
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>