"Ihor" == Ihor Jakowec ihor@autobahn.mb.ca writes:
Ihor> During last night's meeting a question was raised about Ihor> benchmarks for the python language, or how fast it is Ihor> relative to other computer languages. Since I am a computer Ihor> language buff, I will attempt to give a more informative Ihor> answer to this question, (to augment and correct my Ihor> utterance of last evening).
Just for fun, I re-coded your Python version as Common Lisp, and ran the basic C and Python-only versions for comparison. Note that if you grok the syntax OK, the Lisp one is about as concise and legible as the Python, or at least, not nearly as painful to read as the C. Quicker to write too, I bet. <grin>
By the way, your benchmark is about as Python-unfriendly as it could be, being calculation-intensive with lots of function calls. Many real-world apps easily fall into the "fast enough" category with little work.
Anyway, here's the code and times on my system. Oh yeah, I just dumped it out to standard output, so I'd add a couple lines for real file output.
Cheers, Tim
;;;;;;;;;;;;;; table.lisp, using SBCL ;;;;;;;;;;;;;;;;;;;;;
(defun hstry (hs) (let ((length 0) (height hs)) (loop while (> hs 1) do (if (= (mod hs 2) 0) (setf hs (/ hs 2)) (progn (setf hs (+ (* 3 hs) 1)) (if (> hs height) (setf height hs)))) (setf length (1+ length))) (values length height)))
(defun hsdisp (maxTry) (let ((hs 0) (hsMaxLength 0) (hsMaxHeight 0) (hsML '()) (hsMH '()))
(loop while (< hs maxtry) do (setf hs (1+ hs)) (multiple-value-bind (l h) (hstry hs) (if (> l hsMaxLength) (progn (setf hsMaxLength l) (push (list hs hsMaxLength) hsML))) (if (> h hsMaxHeight) (progn (setf hsMaxHeight h) (push (list hs hsMaxHeight) hsMH)))))
(format t "~10,A ~10,A~%" "hs" "length maxima") (dolist (x (reverse hsML)) (format t "~{~10,d ~}~%" x)) (format t "~%~%~10,A ~10,A~%" "hs" "height maxima") (dolist (x (reverse hsMH)) (format t "~{~10,d ~}~%" x)) ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
C: table.c
real 0m0.134s user 0m0.128s sys 0m0.002s
python2.2: table.py
real 0m21.171s user 0m20.237s sys 0m0.022s
python2.3: table.py
real 0m15.826s user 0m14.756s sys 0m0.021s
sbcl: table.lisp
real 0m2.868s user 0m2.259s sys 0m0.001s