all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: emacs evaluating
Date: Sat, 07 Feb 2009 23:23:46 +0100	[thread overview]
Message-ID: <87bptdga31.fsf@galatea.local> (raw)
In-Reply-To: 868woinf00.fsf@timbral.net

Evans Winner <thorne@timbral.net> writes:

> pjb@informatimago.com (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__


  parent reply	other threads:[~2009-02-07 22:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <gmir6e$cno$1@gregory.bnet.hr>
     [not found] ` <gmjvvq$fuv$1@gregory.bnet.hr>
2009-02-07 14:18   ` emacs evaluating Pascal J. Bourguignon
2009-02-07 14:36     ` Xah Lee
2009-02-07 20:55     ` Evans Winner
2009-02-07 21:25       ` Xah Lee
2009-02-07 22:23       ` Pascal J. Bourguignon [this message]
2009-02-07 22:33       ` Michael Ekstrand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bptdga31.fsf@galatea.local \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.