[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Efficiency of local fns and closures



>
>Consider:
>
>(defun foo (x y)
>  (labels ((bar (z)
>	    (some-function y z)))		;free reference to y
>    (bar (some-other-function x))))
>
>I have a two-part question regarding efficiency:
   etc.
>
>-Kurt Godden
> godden@gmr.com

The easy answer is to run it:
I tried 5 versions of a double loop call (1000000 calls):
1.  Empty loop
2.  Original version as above
3.  External function bar
4.  Internal function bar, with 2 arguments, no free reference
5.  unfolded operation of functions (* (1+ z) y))
Times in seconds:
	Symbolics:	Sun 3/280 Lucid 3.0...
1.	 1.753		 2.82	 
2.	22.34		89.02
3.	15.434		62.92
4.	16.401		64.28
5.	 5.46		---
Note that the timing operation was in favor of the Sun, in that only the user
time is included, total time for the Symbolics.
Answers:
External function is more efficient, free variable reference costs time.
(Internal definition without variable reference is almost the same as external
function).
	Keith Price.