Using SBCL 1.4.12, I am looking at Exercise 17.9 from Stuart Shapiro's Common Lisp: An Interactive Approach, and timing a reverse
function applied to a list of 10,000 elements. When I time this function using the same list, the time
function reports a different number of bytes consed each time.
Here is the code for the reverse
function:
(defun reverse2 (l1 l2)
"Returns a list consisting of the members of L1 in reverse order
followed by the members of L2 in original order."
(check-type l1 list)
(check-type l2 list)
(if (endp l1) l2
(reverse2 (rest l1)
(cons (first l1) l2))))
(defun reverse1 (l)
"Returns a copy of the list L1
with the order of members reversed."
(check-type l list)
(reverse2 l '()))
I generated the list in the REPL with:
(defvar *test-list* '())
(dotimes (x 10000)
(setf *test-list* (cons x *test-list*)))
Here are the results of four test-runs:
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
520,386 processor cycles
145,696 bytes consed
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
260,640 processor cycles
178,416 bytes consed
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
279,822 processor cycles
178,416 bytes consed
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
264,700 processor cycles
161,504 bytes consed
The second and third test runs (which were several minutes apart) show the same number of bytes consed, but the other two show different numbers. I expected the timing to vary, but I did not expect the number of bytes consed to vary. I see that the HyperSpec says of the time
function that:
In general, these timings are not guaranteed to be reliable enough for marketing comparisons. Their value is primarily heuristic, for tuning purposes.
But I expected that this applies to timings, not to byte counts. Are the bytes consed values reported by time
unreliable? Is there an optimization behind the scenes that is responsible for this? What am I missing?