I'm trying to discern how much performance gains I would achieve by converting Lisp (SBCL) code to assembly language. The time
function on SBCL keeps giving counterintuitive results even for trivial operations.
On the one hand, running this implementation
(defun test()
(declaim (optimize (speed 3) (safety 0) (debug 0)))
(setf my-array (make-array 10))
(setf (aref my-array 2) 4)
(time (progn
(aref my-array 1)
(aref my-array 3)))
)
reports roughly between 2,200 and 3,000 clock cycles, which in and of itself seems excessive for just a pair of straightforward lookups in a small array.
On the other hand, running this variant under similar conditions
(defun test()
(declaim (optimize (speed 3) (safety 0) (debug 0)))
(setf my-array (make-array 10))
(setf (aref my-array 2) 4)
(time (progn
(aref my-array 1)
(aref my-array 3)))
(time (progn
(aref my-array 1)
(aref my-array 3))) ;just duplicated previous line
)
results in each call to time reporting between 1,200 and 2,000 clock cycles. Hence the performance is twofold odd: still too many cycles for trivial operations, yet a noticeable difference between calling time once versus twice.
In both versions I placed time
inside the function body in order to obviate the overhead of loading the function and preparing for execution. Also I'm omitting the outliers because otherwise the matter looks more volatile. Ranges vary between Linux (Ubuntu 22.4) and Windows, but the issue is essentially the same.
Obviously this method doesn't look promising for deciding on code of much greater complexity. Am I missing something? or is the time built-in function just unreliable for performance measurement?
aref
calls entirely, since the result isn't used and it has no side effects.my-array
a local variable and declaring the type? General arrays are very flexible, soaref
has to call to a function that does the hard work.my-array
is as while the convention for special variables is*earmuffs*
it is not enforced somy-array
may be special and might miss out on optimization possibilities.