Subject: Maxima rationals compared to CL rationals
From: Raymond Toy
Date: Wed, 01 Aug 2007 14:22:33 -0400
Barton Willis wrote:
> Which function will compute 1 + 1/2 + ... + 1/5000 the fastest?
>
> ;; Use Maxima rational numbers with add & div.
>
> (defun $harmonic (n)
> (let ((acc 0))
> (dotimes (i n acc)
> (setq acc (add acc (div 1 (+ 1 i)))))))
>
> ;; Use CL rationals with + and /.
>
> (defun $harmonic2 (n)
> (let ((acc 0))
> (dotimes (i n (cl-rat-to-maxima acc))
> (setq acc (+ acc (/ 1 (+ 1 i)))))))
>
> Using GCL, I get
>
> (%i1) load("harmonic.o")$
>
> (%i2) harmonic(5000)$
> Evaluation took 0.19 seconds (0.19 elapsed)
>
> (%i3) harmonic2(5000)$
> Evaluation took 19.89 seconds (19.89 elapsed)
>
> Is this due to fast code in addk? Or is GCL just slow
> with rational addition and division? Or something else?
> What's the story?
Neat.
FWIW, I ran your code with cmucl:
(%i2) harmonic(5000)$
Evaluation took 0.82 seconds (1.25 elapsed) using 28.333 MB.
(%i3) harmonic2(5000)$
Evaluation took 0.50 seconds (0.69 elapsed) using 17.922 MB.
(%i4)
With clisp on the same machine:
(%i2) harmonic(5000)$
Evaluation took 2.08 seconds (2.30 elapsed) using 16.206 MB.
(%i3) harmonic2(5000)$
Evaluation took 1.29 seconds (1.51 elapsed) using 10.924 MB.
(%i4)
Sounds like gcl is not working as well as it should be. I thought gcl
used gmp which is comparable in speed to clisp's bignum routines.
Ray