Julia is very fast, 0.12 second, 0.60 second for sbcl (lisp)
julia>function haz(n)
s = 0
for i in 1:n
if i % 2 == 0
s = s+1
else
s = s-1
end
end
s
end
haz (generic function with 1 method)
julia> @elapsed haz(10^8)
0.12459633
Compare with lisp sbcl on the same machine:
(defun haz(n)
(let ((s 0))
(declare (optimize (speed 3) (safety 0))
(fixnum n s))
(loop for i fixnum from 1 upto n
do (if (zerop (mod n 2)) (incf n) (decf n))
finally (return s))))
julia>function haz(n) s = 0 for i in 1:n if i % 2 == 0 s = s+1 else s = s-1 end end s end haz (generic function with 1 method)
julia> @elapsed haz(10^8) 0.12459633
Compare with lisp sbcl on the same machine:
(defun haz(n) (let ((s 0)) (declare (optimize (speed 3) (safety 0)) (fixnum n s)) (loop for i fixnum from 1 upto n do (if (zerop (mod n 2)) (incf n) (decf n)) finally (return s))))