[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Efficiency of local fns and closures
Date: Sun, 22 Oct 89 20:10:17 EDT
From: barmar@Think.COM
I was doing loops of 100,000 and 1,000,000 repetitions, ran them a couple
of times and saw consistent results. My functions were pretty simple, but
it's possible that I wasn't factoring everything out. This is similar to
what I used to determine speed of accessing various types of variable in a
local function:
(defun test (x n)
(flet ((test1 (x) (declare (ignore x))) ; access no variable
(test2 (x) x) ; access local variable
(test3 () x)) ; access lexical variable
(without-interrupts (time (loop repeat n do (test1 1))))
(without-interrupts (time (loop repeat n do (test2 1))))
(without-interrupts (time (loop repeat n do (test3))))))
I guess comparing TEST1 to either of the others includes the difference
between RETURN-NIL and RETURN-STACK. But I think it should give an
accurate measurement when comparing TEST2 and TEST3.
More importantly, you are comparing the speed of calling a closure with
one argument and accessing a parent lexical variable, to the speed of
calling a non-closure function with no arguments and accessing a local
variable, and then calling that the difference between accessing a
parent lexical variable and accessing a local variable. The difference
you are reporting is the sum of three differences: two types of called
function, two numbers of arguments, and two types of variable access.