4 ms·
The improved Swift code shown in the original post takes 0.5s to compile with optimizations on my system. It runs in 4-5us per inner loop. The following Common
by metroholografix 7y ago
The improved Swift code shown in the original post takes 0.5s to compile with optimizations on my system. It runs in 4-5us per inner loop.
The following Common Lisp code:
(defun bench-array ()
(declare (optimize (speed 3) (debug 0) (safety 1)))
(let ((arr (make-array 3000)))
(declare (dynamic-extent arr)) ; stack allocate
(loop
:with sum fixnum = 0
:for i fixnum :from 0 :repeat 15 :do
(time
(setf sum
(loop :for k fixnum :from 0 :repeat 3000
:do
(setf (aref arr k) i)
:finally (return (loop :for l fixnum :across arr
:sum l fixnum)))))
(print sum))))
compiles in 0.01s and runs in 5us per inner loop on my system (SBCL).
It has array bounds checking enabled (safety 1 declaration).
If I remove it (safety 0), runtime improves to 2-3us per inner loop.
I'll take Common Lisp over Swift any day of the week :-]