In lisp which is faster a defun or a defmacro

Discussion of Common Lisp

In lisp which is faster a defun or a defmacro

Postby joeish80829 » Sun Nov 10, 2013 7:21 am

title says it all just a quick ?=)
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: In lisp which is faster a defun or a defmacro

Postby marcoxa » Sun Nov 10, 2013 7:53 am

It depends on the kind of code you are producing with a macro.
Marco Antoniotti
marcoxa
 
Posts: 69
Joined: Thu Aug 14, 2008 6:31 pm

Re: In lisp which is faster a defun or a defmacro

Postby sylwester » Sun Nov 10, 2013 1:27 pm

In some circumstances where there would be differences between defmacro and defun, defmacro would be the fastest (since you could constant fold).
However, the best practice is to make it a function as long as it's possible to do what you want with functions.

The 3 language agnostic rules of optimization is:
  1. Don't
  2. Don't... yet.
  3. Profile before you optimize so you don't end up optimizing blindly
Premature optimization is a bad thing so don't start in that end :-)
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 83
Joined: Mon Jul 11, 2011 2:53 pm

Re: In lisp which is faster a defun or a defmacro

Postby edgar-rft » Sun Nov 10, 2013 5:48 pm

The only thing that can be said for sure is that "defun" can be typed faster than "defmacro" because there are three letters less to type. Everything else entirely depends on what you define using "defun" or "defmacro". Wihout any context this question makes not the slightest sense.
edgar-rft
 
Posts: 154
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: In lisp which is faster a defun or a defmacro

Postby joeish80829 » Sun Nov 10, 2013 7:11 pm

what is constant folding btw......I found this definition "During compilation, the form is evaluated and replaced by its value. Forms that can be constant folded include symbols defined by usingdefconstant, atoms other than symbols, lists whose car isquote, and constant numerical expressions." can u show examples of all these....and is there online link you can provide that will help me learn to code to take more advantage of constant folding
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: In lisp which is faster a defun or a defmacro

Postby pjstirling » Tue Nov 12, 2013 6:35 am

Constant folding is a simple technique for increasing the execution speed of code where the compiler uses the fact that a particular function when invoked with particular arguments will always return the same result. When the compiler can work out that result at compile time it can simply put the constant value into the variable at run-time without doing any calculations at run-time.

Calculating sin(x) takes quite a lot of calculations for a computer, but for any constant value of x, it will have a constant result. An example would be (SIN (/ PI 2)), which always has the result 1.0d0. A compiler can use this fact to replace (SIN (/ PI 2)) with 1.0d0 and save these 'wasted' calculations. Things are slightly more complicated in common-lisp though, because although you do want to constant-fold SIN when possible, you also need to be able to call it at run time (in particular you might want to (MAPCAR #'SIN ...), so it can't be a pure macro, or function. The third way in common-lisp is compiler-macros, rather than using DEFMACRO you would use normal DEFUN, which gets used for run-time calculations, and a compiler-macro (DEFINE-COMPILER-MACRO), which can be used for constant-folding, which means that SIN becomes both a macro and a function.

As another example, I have an html generation library that aggressively folds the string constants used in the FORMAT calls which I need to get around to putting on github
pjstirling
 
Posts: 77
Joined: Sun Nov 28, 2010 4:21 pm

Re: In lisp which is faster a defun or a defmacro

Postby joeish80829 » Tue Nov 12, 2013 7:00 am

Thanks for the info ....Id love to see your library when you put it up
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests