Subject: Maxima rationals compared to CL rationals
From: Barton Willis
Date: Wed, 1 Aug 2007 12:38:02 -0500
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?
Barton