Author Topic: About function FUNCTION (AutoLISP/Visual LISP IDE)  (Read 2371 times)

0 Members and 1 Guest are viewing this topic.

Logan

  • Newt
  • Posts: 41
About function FUNCTION (AutoLISP/Visual LISP IDE)
« on: August 25, 2017, 04:19:00 PM »
Hi all.

According to the help documentation the function function optimize the lambda expression...

Quote
Tells the Visual LISP compiler to link and optimize an argument as if it were a built-in function

Signature
(function symbol | lambda-expr)

Remarks
The function function is identical to the quote function, except it tells the Visual LISP compiler to link and optimize the argument as if it were a built-in function or defun.
Compiled lambda expressions that are quoted by function will contain debugging information when loaded into the Visual LISP IDE.

Ok.
Can anyone tell me if this optimization can be measured?  :rolleyes2:

I always use the function function in set of lambda expressions just in case.
I'm sorry if this question has been answered before or if it does not make sense the question.

cheers

ronjonp

  • Needs a day job
  • Posts: 7529
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #1 on: August 25, 2017, 04:51:53 PM »
Use Michael's Benchmark code and test for yourself  8)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #2 on: August 25, 2017, 04:52:30 PM »
Hi,

You can try to write 2 similar defun, one using quote and the other using function:
Code - Auto/Visual Lisp: [Select]
  1. (defun quoteLambda (l)
  2.   (mapcar '(lambda (x) (* x x)) l)
  3. )
  4.  
  5. (defun functionLambda (l)
  6.   (mapcar (function (lambda (x) (* x x))) l)
  7. )
Save them in a .lsp file.
Compile the file into .vlx
Run a benchmark calling the 2 functions, you'd see functionLambda running 2 times faster than quoteLambda.

$ (setq l '(1 2 3 4 5 6 7 8 9 ))
(1 2 3 4 5 6 7 8 9)
_$ (benchmark '((quoteLambda l) (functionLambda l)))
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

    (FUNCTIONLAMBDA L).....1029 / 2.2 <fastest>
    (QUOTELAMBDA L)........2262 / 1 <slowest>
Speaking English as a French Frog

Logan

  • Newt
  • Posts: 41
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #3 on: August 25, 2017, 05:24:01 PM »
Wow 
I am really very happy with the answers. :yay!:
It was exactly what I was looking for.

Ron, thanks for showing me this code Ron, he's really very helpful.

Many thanks for your generosity Gile. I'm sure your answer will help many beginners like me.

Cheers

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #4 on: August 25, 2017, 08:05:21 PM »
I'm just putting this here for my own understanding really but I think the key word here is 'compile', when an interpreter or compiler eval's your Lisp code as text it parses it and creates bytecodes (like machine assembly instructions) and puts them on the program stack which makes the execution of these functions much faster when needed.

So, when the compiler sees the 'function' directive it can create the bytecodes like a normal function whereas the quoted expression version (which is just data list at this point) must be evaluated to bytecodes each time it's used.

Hope that made sense :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

ronjonp

  • Needs a day job
  • Posts: 7529
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #5 on: August 26, 2017, 12:06:13 AM »
I'm just putting this here for my own understanding really but I think the key word here is 'compile', when an interpreter or compiler eval's your Lisp code as text it parses it and creates bytecodes (like machine assembly instructions) and puts them on the program stack which makes the execution of these functions much faster when needed.

So, when the compiler sees the 'function' directive it can create the bytecodes like a normal function whereas the quoted expression version (which is just data list at this point) must be evaluated to bytecodes each time it's used.

Hope that made sense :)
Nice. That helps my understanding. *cheers*

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #6 on: August 28, 2017, 11:44:44 AM »
Hi all.

According to the help documentation the function function optimize the lambda expression...

Quote
Tells the Visual LISP compiler to link and optimize an argument as if it were a built-in function

Signature
(function symbol | lambda-expr)

Remarks
The function function is identical to the quote function, except it tells the Visual LISP compiler to link and optimize the argument as if it were a built-in function or defun.
Compiled lambda expressions that are quoted by function will contain debugging information when loaded into the Visual LISP IDE.

...

Logan,
Just for your information, TheSwamp has a built in Autolisp help page which you can read/link to. The web page is located in my directory on the server. The document is hyper-linked and all that good stuff. Below is a link how you can access a given function in the document/page.

http://www.theswamp.org/~john/avlisp/#function

Type the function name you want to link to or read up on after the hash mark.

Leave the hash mark off and you will get the index of the page.
http://www.theswamp.org/~john/avlisp/

Also, click any blue function name in a code block to access the same help page.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Logan

  • Newt
  • Posts: 41
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #7 on: August 29, 2017, 08:03:10 AM »
Se7en, thank you very much for the information.
It will be of great use to me.
That's why I love this place.
I am very grateful to all of you who contribute to my learning.
Thank you all. 

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #8 on: August 29, 2017, 08:39:07 AM »
No problem.

IMO that help file is very helpful and very useful; it's fast and self contained (you can download it and have a copy on your hard drive). Not to mention it's linked to the code blocks so if anyone uses a function in their code you can just click the function name and read up on it (that's why I wanted all users to use the custom code tags we created; so other people have an easier time learning from their code).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Logan

  • Newt
  • Posts: 41
Re: About function FUNCTION (AutoLISP/Visual LISP IDE)
« Reply #9 on: August 29, 2017, 02:23:57 PM »
No problem.

IMO that help file is very helpful and very useful; it's fast and self contained (you can download it and have a copy on your hard drive). Not to mention it's linked to the code blocks so if anyone uses a function in their code you can just click the function name and read up on it (that's why I wanted all users to use the custom code tags we created; so other people have an easier time learning from their code).

It really is a great idea.