Re: Performance of (fn [] ...)?
Creating a small object like that is cheap on the JVM. There are much better places to put optimization effort. On Wed, Feb 18, 2009 at 1:07 PM, Michel Salim wrote: > > > > On Feb 18, 3:17 am, Laurent PETIT wrote: > > Hello, > > > > 2009/2/18 CuppoJava > > > > > > > > > Hi, > > > I've noticed that I'm creating a lot of maps of functions, and I'm > > > wondering if there's a performance penalty for this. > > > > > ie. > > > (defn create_fn [] > > > (fn [] (println "hi"))) > > > > If you use AOT compilation, you'll see that this code will add 2 new > classes > > to the namespace it is declared in : one class for create_fn itself, and > one > > anonymous class for (fn [] (println "hi")) > > > > > ((create_fn)) <--- Does this "create" a new function every-time it's > > > called? Or is the function code cached somewhere? How much of a > > > performance penalty does this incur? > > > > Each call to create_fn will create a new instance (object) of the > anonymous > > class for (fn [] (println "hi")) > > > Is this something that will eventually be optimized? This anonymous > function does not capture any variable, and therefore can just be > instantiated once. > > Regards, > > -- > Michel S. > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
On Feb 18, 3:17 am, Laurent PETIT wrote: > Hello, > > 2009/2/18 CuppoJava > > > > > Hi, > > I've noticed that I'm creating a lot of maps of functions, and I'm > > wondering if there's a performance penalty for this. > > > ie. > > (defn create_fn [] > > (fn [] (println "hi"))) > > If you use AOT compilation, you'll see that this code will add 2 new classes > to the namespace it is declared in : one class for create_fn itself, and one > anonymous class for (fn [] (println "hi")) > > > ((create_fn)) <--- Does this "create" a new function every-time it's > > called? Or is the function code cached somewhere? How much of a > > performance penalty does this incur? > > Each call to create_fn will create a new instance (object) of the anonymous > class for (fn [] (println "hi")) > Is this something that will eventually be optimized? This anonymous function does not capture any variable, and therefore can just be instantiated once. Regards, -- Michel S. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
Thanks for the replies. I have no qualms about creating functions now. =) On Feb 18, 3:15 am, Michael Wood wrote: > On Wed, Feb 18, 2009 at 8:50 AM, David Nolen wrote: > > new MBP 2.53ghz > > > (defn create-fn [] > > (fn [] (println "hi"))) > > > (time (dotimes [x 4000] > > (create-fn))) > > >> "Elapsed time: 1034.409 msecs" > > > Hopefully you don't need 40,000,000 functions in less than a second ;) > > Well that takes about 12 seconds for me on a dual CPU P3 1GHz > (/proc/cpuinfo says 930MHz?). :) > > -- > Michael Wood --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
On Wed, Feb 18, 2009 at 8:50 AM, David Nolen wrote: > new MBP 2.53ghz > > (defn create-fn [] > (fn [] (println "hi"))) > > (time (dotimes [x 4000] > (create-fn))) > >> "Elapsed time: 1034.409 msecs" > > Hopefully you don't need 40,000,000 functions in less than a second ;) Well that takes about 12 seconds for me on a dual CPU P3 1GHz (/proc/cpuinfo says 930MHz?). :) -- Michael Wood --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
CuppoJava a écrit : > Hi, > I've noticed that I'm creating a lot of maps of functions, and I'm > wondering if there's a performance penalty for this. > > ie. > (defn create_fn [] > (fn [] (println "hi"))) > > ((create_fn)) <--- Does this "create" a new function every-time it's > called? Or is the function code cached somewhere? How much of a > performance penalty does this incur? > > Thanks for the help > Last time I ckecked (http://clj-me.blogspot.com/2008/11/surprising.html) it was cheaper to create a closure than a vector or a list. Christophe -- Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.blogspot.com/ (en) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
OK, should have read the doc more carefully, thanks, -- laurent 2009/2/18 Christophe Grand > > Laurent PETIT a écrit : > > When I call test-create-fn with 40, the elapsed time falls > > down to zero : I suspect it does nothing, and in the same time it does > > not seem to correctly crash by throwing an exception ? > > > > I've tested the correct handling of that high numeric values by > > clojure, it sounds ok : > > user=> (println 40) > > 40 > > nil > > user=> (dec 40) > > 39 > > > > > > Can someone explain me what happens here ? > dotimes assumes its bound to be an int. > user=> (int 40) > -294967296 > > 0 being greater than -294967296 the iteration exits immediatly. > > -- > Professional: http://cgrand.net/ (fr) > On Clojure: http://clj-me.blogspot.com/ (en) > > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
Laurent PETIT a écrit : > When I call test-create-fn with 40, the elapsed time falls > down to zero : I suspect it does nothing, and in the same time it does > not seem to correctly crash by throwing an exception ? > > I've tested the correct handling of that high numeric values by > clojure, it sounds ok : > user=> (println 40) > 40 > nil > user=> (dec 40) > 39 > > > Can someone explain me what happens here ? dotimes assumes its bound to be an int. user=> (int 40) -294967296 0 being greater than -294967296 the iteration exits immediatly. -- Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.blogspot.com/ (en) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
Hello, There's something I don't understand : I've used this code : (ns clojure.examples.createfn) (defn create-fn [] (fn [] (println "hi"))) (defn test-create-fn [n] (time (dotimes [x n] (create-fn and made these tests : user=> (require 'clojure.examples.createfn :reload) nil user=> (clojure.examples.createfn/test-create-fn 4000) "Elapsed time: 356.096374 msecs" nil user=> (clojure.examples.createfn/test-create-fn 40) "Elapsed time: 0.013218 msecs" nil When I call test-create-fn with 40, the elapsed time falls down to zero : I suspect it does nothing, and in the same time it does not seem to correctly crash by throwing an exception ? I've tested the correct handling of that high numeric values by clojure, it sounds ok : user=> (println 40) 40 nil user=> (dec 40) 39 Can someone explain me what happens here ? 2009/2/18 David Nolen > new MBP 2.53ghz > > (defn create-fn [] > (fn [] (println "hi"))) > > (time (dotimes [x 4000] > (create-fn))) > > > "Elapsed time: 1034.409 msecs" > > Hopefully you don't need 40,000,000 functions in less than a second ;) > > On Wed, Feb 18, 2009 at 1:16 AM, CuppoJava wrote: > >> (defn create_fn [] >> (fn [] (println "hi"))) >> > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
Hello, 2009/2/18 CuppoJava > > Hi, > I've noticed that I'm creating a lot of maps of functions, and I'm > wondering if there's a performance penalty for this. > > ie. > (defn create_fn [] > (fn [] (println "hi"))) If you use AOT compilation, you'll see that this code will add 2 new classes to the namespace it is declared in : one class for create_fn itself, and one anonymous class for (fn [] (println "hi")) > ((create_fn)) <--- Does this "create" a new function every-time it's > called? Or is the function code cached somewhere? How much of a > performance penalty does this incur? Each call to create_fn will create a new instance (object) of the anonymous class for (fn [] (println "hi")) HTH, -- Laurent > > > Thanks for the help > -Patrick > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Performance of (fn [] ...)?
new MBP 2.53ghz (defn create-fn [] (fn [] (println "hi"))) (time (dotimes [x 4000] (create-fn))) > "Elapsed time: 1034.409 msecs" Hopefully you don't need 40,000,000 functions in less than a second ;) On Wed, Feb 18, 2009 at 1:16 AM, CuppoJava wrote: > (defn create_fn [] > (fn [] (println "hi"))) > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Performance of (fn [] ...)?
Hi, I've noticed that I'm creating a lot of maps of functions, and I'm wondering if there's a performance penalty for this. ie. (defn create_fn [] (fn [] (println "hi"))) ((create_fn)) <--- Does this "create" a new function every-time it's called? Or is the function code cached somewhere? How much of a performance penalty does this incur? Thanks for the help -Patrick --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---