Subject: Re: calling compiled lambda forms
From: rpw3@rpw3.org (Rob Warnock)
Date: Sun, 01 Jul 2007 00:57:01 -0500
Newsgroups: comp.lang.lisp
Message-ID: <D7CdnWKpg5uw3xrbnZ2dnUVZ_vmqnZ2d@speakeasy.net>
Kent M Pitman  <pitman@nhplace.com> wrote:
+---------------
| rpw3@rpw3.org (Rob Warnock) writes:
| > Tamas Papp <tkpapp@gmail.com> wrote:
| > +---------------
| > | I am not writing any program, just going through On Lisp and playing
| > | around in the REPL.
| > +---------------
| > 
| > O.k., in that case others have answered you -- FUNCALL is your friend:
| >     > (funcall (compile nil (lambda (x y) (1- (expt x y)))) 2 100)
| >     ; Compiling LAMBDA (X Y): 
| >     ; Compiling Top-Level Form: 
| >     1267650600228229401496703205375
| 
| With the caveat that compiling just to call something once is horribly 
| expensive.  At least TRY to do
|  (defvar *my-function* (compile nil '(lambda (x y) ...)))
+---------------

Yes, yes, but he *did* say he was just playing around in the REPL.  ;-}  ;-}

I confess that even I do that sometimes, although usually for rather
different reasons. E.g., the other day I found myself typing this
several times in a row:

    > (disassemble (compile nil (lambda () (dotimes (i 2000000000)))))
    ...[unimportant stuff]...
	 BF1: L0:   INC     EAX                  ; [:BLOCK-START]
	 BF2: L1:   MOV     ECX, EAX             ; [:BLOCK-START]
	 BF4:       CMP     ECX, 2000000000
	 BFA:       JB      L0
    ...[unimportant stuff]...
    > 

to try to figure out why (TIME (DOTIMES (I 2000000000))) *sometimes*
takes 2 CPU clock cycles per iteration on my Athlon and other times
takes 3 clocks. [Tentative conclusion: It takes 3 cycles/count when
the inner loop happens to span a cache block boundary.]


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607