1

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?

Barmar
787k57 gold badges552 silver badges664 bronze badges
asked 2 days ago
3
  • I'm surprised the compiler isn't optimizing out the aref calls entirely, since the result isn't used and it has no side effects. Commented yesterday
  • Have you tried making my-array a local variable and declaring the type? General arrays are very flexible, so aref has to call to a function that does the hard work. Commented yesterday
  • SBCL compiles to native CPU instructions so what you see is the CPU optimizations due to caching and cache misses etc. Also it's not obvious to me what my-array is as while the convention for special variables is *earmuffs* it is not enforced so my-array may be special and might miss out on optimization possibilities. Commented 13 hours ago

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.