help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: emacs evaluating


From: Pascal J. Bourguignon
Subject: Re: emacs evaluating
Date: Sat, 07 Feb 2009 23:23:46 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

Evans Winner <address@hidden> writes:

> address@hidden (Pascal J. Bourguignon) writes:
>
>     To compile a function: (byte-compile 'your-function) C-x
>     C-e
>
> I had never thought of this.  I think I really must not
> understand compilation very clearly.  I think I grasp that
> an entire file full of functions will load faster if it is
> compiled first, but what does interactively compiling a
> single function buy me? 

Depends.

> Does it exist in a different form
> in the image when you do that?  

Yes.

Evaluate the following forms:

(defun fact (x)
  (if (< x 1)
      (/ x 0)
      (* x (fact (1- x)))))

(symbol-function 'fact)

(byte-compile 'fact)

(symbol-function 'fact)



> Does it execute faster?

Possibly.  Well the purpose is definitely to make it faster, and in
most cases, it might succeed.

However, if you must take into account the time taken by byte-compile
itself, it might not be faster globally.  In some cases, it may be
better not to compile.


> (This is not a facetious question, by the way.)  Er, maybe
> it has to do with debugging macros or something...?

It has an impact on the debugging yes.  Compiled functions leave less
information to the debugger, so it may be harder to debug them.
Evaluate the following forms in turn.

(defun fact (x)
  (if (< x 1)
      (/ x 0)
      (* x (fact (1- x)))))

(setq debug-on-error t)

(fact 5)

(byte-compile 'fact)

(fact 5)

(you can type q to exit from the debugger ; this is not a form)



Then try to evaluate the following forms in turn:


(defun get-internal-real-time ()
  (destructuring-bind (hi lo msec) (current-time)
    (+ (/ msec 1000000.0) lo (* hi 65536.0))))


(defmacro time (&rest body)
  "Common-Lisp:  time evaluates form in the current environment (lexical and \
dynamic). A call to time can be compiled.
DO:      time prints various timing data and other information to trace output.
         The nature and format of the printed information is
         implementation-defined . Implementations are encouraged to provide
         such information as elapsed real time, machine run time, and storage
         management statistics.
"
  (let ((start  (gensym))
        (result (gensym))
        (stop   (gensym))
        (time   (gensym)))
    `(let* ((,start  (get-internal-real-time))
            (,result (progn ,@body))
            (,stop   (get-internal-real-time)) 
            (,time   (- ,stop ,start)) )
       (insert (format "Time: %e ms\n" (* 1000 ,time))))))


(defun fact (x)
  (if (< x 1)
      1
      (* x (fact (1- x)))))

(time (fact 2000))

(byte-compile 'fact)
(time (fact 2000))


-- 
__Pascal Bourguignon__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]