I don't yet know OCaml well and I don't care to take the time to consider the relative quality of these two chunks of code, but here's some Lisp benchmark code:
(defun main ()
(flet ((read-int (x)
(let ((int 0) (sgn 1) (eof t))
(declare (type fixnum int sgn))
(loop for byte of-type fixnum = (char-code (read-char x nil #\null))
for num of-type fixnum = (- byte (char-code #\0))
when (= byte (char-code #\-)) do (setq sgn -1)
when (and (/= byte (char-code #\-)) (or (< num 0) (>= num 10))) do (return (if eof nil (* int sgn)))
when (/= byte (char-code #\-)) do
(if eof (setq eof nil))
(setq int (+ (the fixnum (* int 10)) num))))))
(princ
(loop for int = (read-int *standard-input*)
when (not (null int)) sum (the fixnum int) into res
when (null int) do (return res)))
(terpri)))
And here's the equivalent OCaml for the same benchmark:
let sum = ref 0
let rec loop () = sum := !sum + int_of_string (input_line stdin); loop ();;
try loop () with End_of_file -> Printf.printf "%d\n" !sum
...
Ouch!
last updated 2 years ago
#
history