<html><head></head><body><div class="yahoo-style-wrap" style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div dir="ltr" data-setdir="false">Does anyone know if AREF is an 'expensive' operation? I've got two versions of a function that differ, mostly, in what they loop over:<br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><div><div><font face=""lucida console", sans-serif">(defun ep1 (coefficients x)</font></div><div dir="ltr" data-setdir="false"><font face=""lucida console", sans-serif">"SIMPLE-ARRAY version of coefficients."<br></font></div><font face=""lucida console", sans-serif">  (declare (double-float x)((simple-array double-float) coefficients)<br>       (optimize(speed 3)(safety 0)))<br>  (let ((sum 0d0))<br>    (declare (double-float sum))<br>    (dotimes (index (length coefficients))<br>      (setf sum (+ (aref coefficients index)<br>                (* x sum))))<br>    (the double-float sum)))</font></div></div><div dir="ltr" data-setdir="false"><font face=""lucida console", sans-serif"><br></font></div><div dir="ltr" data-setdir="false"><div><div><font face=""lucida console", sans-serif">(defun ep2 (coefficients x)</font></div><div dir="ltr" data-setdir="false"><font face=""lucida console", sans-serif">"LIST version of coefficients."<br></font></div><font face=""lucida console", sans-serif">  (declare (optimize(speed 3)(safety 0)))<br>  (assert (typep coefficients 'list) () "COEFFICIENTS must be a LIST")<br>     (let((sum (car coefficients)))<br>       (declare (double-float sum x))<br>       (loop for i double-float in (cdr coefficients)<br>      do (setf sum (+ i (* x sum))))<br>       sum))</font></div><div><br></div><div dir="ltr" data-setdir="false">The list version executes faster than the simple-array version, which I find surprising. Not by a great deal, but consistently faster. As I understand the layout of the LIST version, the double-float is in the CAR of the CONS, saving a pointer dereference to get the value, but the loop still needs to dereference the CDR pointer. In the SIMPLE-ARRAY version, the DOUBLE-FLOATS are laid out in contiguous memory, which in theory should be faster to access. I think I've seen similar behaviour before, with loops over short lists out-performing arrays up to a certain length.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">The only thing I can think of is that AREF might be a more expensive operation than reading the car, so for short lists the pointer dereferencing doesn't make a significant difference. Does that sound like a reasonable theory?<br></div></div></div></body></html>