Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is declaring anonymous functions expensive in Clojure?

As Clojure programmers we use lots of anonymous functions without thinking it's cost.

What are the relative costs of creating and using anonymous functions in clojure?

like image 834
Ertuğrul Çetin Avatar asked Oct 04 '16 22:10

Ertuğrul Çetin


1 Answers

Clojure compiles all functions, anonymous or named, the same way. It then stores a pointer to that function in a namespace (contained in a var) so others can find it later.

There is no cost difference in compile time between functions that are compiled and used as anonymous functions, vs functions that are compiled, then have a pointer to them stored in a var that is part of a namespace.

When anonymous functions used at runtime, most of the time (perhaps always) they are created by making closures (the objects) so the cost of creating them is some memory and a little time.

If you are calling eval in time critical loops of course you can create the same problems in Clojure that you can make in any other language.

like image 173
Arthur Ulfeldt Avatar answered Oct 11 '22 13:10

Arthur Ulfeldt