Hi everyone,My advice to you is not to use lambda and define a normal function
I have a couple of functions that take a (lambda) function as a variable.
For example here is a function that gets repeated an n amount of times and puts the return values of that function into a list.Code - Auto/Visual Lisp: [Select]
It can be called like this:Code - Auto/Visual Lisp: [Select]
But because I have put an eval function in the first line, it can also be called with a quote before the function:Code - Auto/Visual Lisp: [Select]The second way seems more common in autolisp because of functions like apply and mapcar needing a quoted function.
But it does make my function a bit slower because of this extra step.
My question is:
What are the advantages of using a quoted function instead of a plain one?
Should I keep the eval in there or is it better to take that line out? Or is it just personal preference?
As far as I know, the quote will slow your procedure down because the preprocessor has to create the definition over each iteration vs look up the function (but I could be off).Thanks for your answer.
My advice to you is not to use lambda and define a normal function
Hi,Hi gile,
How far is it faster?
IMHO, if speed is really the issue, forget LISP and switch to .NET or ObjectARX.
In this case, I'd rather try to be consistent with other AutoLISP higher order functions and use the quoted function with the (setq f (eval f)) expression (even it is a little slower).
See this reply (http://www.theswamp.org/index.php?topic=57401.msg608942#msg608942).
(eval removed, no quote, lambda function).....1047 / 1.85 <fastest>
(eval removed, no quote, normal function).....1093 / 1.77
(with eval, no quote, lambda function).....1843 / 1.05
(with eval, no quote, normal function).....1844 / 1.05
(with eval, quoted, normal function).....1859 / 1.04
(with eval, quoted, lambda function).....1937 / 1 <slowest>
eval is run source code,it is very slowHi,Hi gile,
How far is it faster?
IMHO, if speed is really the issue, forget LISP and switch to .NET or ObjectARX.
In this case, I'd rather try to be consistent with other AutoLISP higher order functions and use the quoted function with the (setq f (eval f)) expression (even it is a little slower).
See this reply (http://www.theswamp.org/index.php?topic=57401.msg608942#msg608942).
Your post was the reason I started thinking about this in the first place. :lol:
Just for consistency is a pretty good reason, I was wondering if there were more reasons to use quoted functions. But none so far.
For this simple function, remove the eval is up to 1.85 times faster.
I'm guessing for more elaborate functions the difference will be much much smaller.Code: [Select](eval removed, no quote, lambda function).....1047 / 1.85 <fastest>
(eval removed, no quote, normal function).....1093 / 1.77
(with eval, no quote, lambda function).....1843 / 1.05
(with eval, no quote, normal function).....1844 / 1.05
(with eval, quoted, normal function).....1859 / 1.04
(with eval, quoted, lambda function).....1937 / 1 <slowest>
eval is run source code,it is very slowWhat does mean with "very slow"?
0.02 ms? That means your expression is not complex enough. There is a big difference between compiled execution and interpreted execution.eval is run source code,it is very slowWhat does mean with "very slow"?
Do not focus on the "1.85 times faster".
From the test I did, the difference between using or not the (eval ...) expression is less than 0.02 milliseconds.
_$ (benchmark '(
((lambda (x) (repeatToListQuoted 5 '(lambda () (setq x (1+ x))))) 0)
((lambda (x) (repeatToListUnquoted 5 (lambda () (setq x (1+ x))))) 0)))
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):
((LAMBDA (X) (REPEATTOLISTUNQUOTED 5...).....1641 / 1.35 <fastest>
((LAMBDA (X) (REPEATTOLISTQUOTED 5 (...).....2219 / 1.00 <slowest>
_$ (/ (- 2219. 1641.) 32768)
0.0176392
_$ (benchmark '(
((lambda (x) (repeatToListQuoted 100 '(lambda () (setq x (1+ x))))) 0)
((lambda (x) (repeatToListUnquoted 100 (lambda () (setq x (1+ x))))) 0)))
Benchmarking ................Elapsed milliseconds / relative speed for 8192 iteration(s):
((LAMBDA (X) (REPEATTOLISTUNQUOTED 1...).....1156 / 1.11 <fastest>
((LAMBDA (X) (REPEATTOLISTQUOTED 100...).....1281 / 1.00 <slowest>
_$ (/ (- 1281. 1156.) 8192)
0.0152588
_$ (benchmark '(
((lambda (x) (repeatToListQuoted 1000 '(lambda () (setq x (1+ x))))) 0)
((lambda (x) (repeatToListUnquoted 1000 (lambda () (setq x (1+ x))))) 0)))
Benchmarking .............Elapsed milliseconds / relative speed for 1024 iteration(s):
((LAMBDA (X) (REPEATTOLISTQUOTED 100...).....1062 / 1.02 <fastest>
((LAMBDA (X) (REPEATTOLISTUNQUOTED 1...).....1079 / 1.00 <slowest>
_$ (/ (- 1079. 1062.) 1024)
0.0166016
We were not discussing about compiled vs interpreted.If you run the source code directly, the speed should be little different, because it is the source code. It is recommended that you compile into Fas file and then compare the running speed. The compiled Eval still runs the source code.
We were just talking about the cost of a (eval ...) expression while passing a quoted function vs passing an unquoted function.
The tests I did were with something similar to the OP:Code - Auto/Visual Lisp: [Select]
Here're some benchmark results with the ellpsed milliseconds calculation for 1 iteration (results are less than 0.02).Code: [Select]_$ (benchmark '(
((lambda (x) (repeatToListQuoted 5 '(lambda () (setq x (1+ x))))) 0)
((lambda (x) (repeatToListUnquoted 5 (lambda () (setq x (1+ x))))) 0)))
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):
((LAMBDA (X) (REPEATTOLISTUNQUOTED 5...).....1641 / 1.35 <fastest>
((LAMBDA (X) (REPEATTOLISTQUOTED 5 (...).....2219 / 1.00 <slowest>
_$ (/ (- 2219. 1641.) 32768)
0.0176392
_$ (benchmark '(
((lambda (x) (repeatToListQuoted 100 '(lambda () (setq x (1+ x))))) 0)
((lambda (x) (repeatToListUnquoted 100 (lambda () (setq x (1+ x))))) 0)))
Benchmarking ................Elapsed milliseconds / relative speed for 8192 iteration(s):
((LAMBDA (X) (REPEATTOLISTUNQUOTED 1...).....1156 / 1.11 <fastest>
((LAMBDA (X) (REPEATTOLISTQUOTED 100...).....1281 / 1.00 <slowest>
_$ (/ (- 1281. 1156.) 8192)
0.0152588
_$ (benchmark '(
((lambda (x) (repeatToListQuoted 1000 '(lambda () (setq x (1+ x))))) 0)
((lambda (x) (repeatToListUnquoted 1000 (lambda () (setq x (1+ x))))) 0)))
Benchmarking .............Elapsed milliseconds / relative speed for 1024 iteration(s):
((LAMBDA (X) (REPEATTOLISTQUOTED 100...).....1062 / 1.02 <fastest>
((LAMBDA (X) (REPEATTOLISTUNQUOTED 1...).....1079 / 1.00 <slowest>
_$ (/ (- 1079. 1062.) 1024)
0.0166016