Re: primitive meta data
Thanks for the advices. The unchecked version run as fast as java. Before I go solve some interesting problem I have to learn the language ;) Thanks anyway. On Wed, Oct 15, 2008 at 12:04 PM, Chouser <[EMAIL PROTECTED]> wrote: > > On Wed, Oct 15, 2008 at 7:08 AM, Parth Malwankar > <[EMAIL PROTECTED]> wrote: > > > > On Oct 15, 8:34 am, Islon <[EMAIL PROTECTED]> wrote: > >> > >> (defn dumb-test [] > >> (let [#^Float f2 567.09723] > >> (loop [#^Float f 1.8, i 1000] > >> (if (zero? i) > >> f > >> (recur (/ f f2) (dec i)) > > The type hints aren't really helping there, I think. #^Float might > help if you were calling a Java function and wanted to avoid the > runtime reflection lookup cost, but you're only calling Clojure > functions so it doesn't help. > > On my machine, about 180 msecs > > To get unboxed primitives, you have to do more like what Parth did: > > > (defn dumb-test [] > > (let [f2 (float 567.09723)] > >(loop [f (float 1.2), i (long 1000)] > > (if (zero? i) > >f > >(recur (/ f f2) (dec i)) > > On my machine that's 48 msecs. > > But we can do a bit better, just by using unchecked-dec: > > (defn dumb-test [] > (let [f2 (float 567.09723)] > (loop [f (float 1.2), i (long 1000)] > (if (zero? i) > f >(recur (/ f f2) (unchecked-dec i)) > > That's 24 msecs for me. > > But I don't know how useful these kinds of micro-benchmarks really > are. Clojure's "fast enough" so let's go solve some interesting > problems... > > --Chouser > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: primitive meta data
On Oct 15, 11:04 am, Chouser <[EMAIL PROTECTED]> wrote: > On Wed, Oct 15, 2008 at 7:08 AM, Parth Malwankar > > <[EMAIL PROTECTED]> wrote: > > > On Oct 15, 8:34 am, Islon <[EMAIL PROTECTED]> wrote: > > >> (defn dumb-test [] > >> (let [#^Float f2 567.09723] > >> (loop [#^Float f 1.8, i 1000] > >> (if (zero? i) > >> f > >> (recur (/ f f2) (dec i)) > > The type hints aren't really helping there, I think. #^Float might > help if you were calling a Java function and wanted to avoid the > runtime reflection lookup cost, but you're only calling Clojure > functions so it doesn't help. > > On my machine, about 180 msecs > > To get unboxed primitives, you have to do more like what Parth did: > > > (defn dumb-test [] > > (let [f2 (float 567.09723)] > >(loop [f (float 1.2), i (long 1000)] > > (if (zero? i) > >f > >(recur (/ f f2) (dec i)) > > On my machine that's 48 msecs. > > But we can do a bit better, just by using unchecked-dec: > > (defn dumb-test [] > (let [f2 (float 567.09723)] >(loop [f (float 1.2), i (long 1000)] > (if (zero? i) >f >(recur (/ f f2) (unchecked-dec i)) > > That's 24 msecs for me. > > But I don't know how useful these kinds of micro-benchmarks really > are. Clojure's "fast enough" so let's go solve some interesting > problems... > Exactly. It's likely that that 24 msecs is the same time as the original Java would be on your box. It's already been demonstrated that with the -server JIT, Clojure can, in inner loops involving primitives, equal the speed of Java. So everyone can rest assured that that capability is there should they need it. The way to get it is as you've demonstrated, with primitive casts. Type hints are only for helping the compiler with Java method calls. But until there's a real performance challenge that's the bottleneck in a real application, there's no reason to get involved in this kind of micro-optimization - it's really a waste of time. Writing code in the clearest, most high-level way is best, and as you say, in Clojure, is usually fast enough. Rich --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: primitive meta data
On Wed, Oct 15, 2008 at 7:08 AM, Parth Malwankar <[EMAIL PROTECTED]> wrote: > > On Oct 15, 8:34 am, Islon <[EMAIL PROTECTED]> wrote: >> >> (defn dumb-test [] >> (let [#^Float f2 567.09723] >> (loop [#^Float f 1.8, i 1000] >> (if (zero? i) >> f >> (recur (/ f f2) (dec i)) The type hints aren't really helping there, I think. #^Float might help if you were calling a Java function and wanted to avoid the runtime reflection lookup cost, but you're only calling Clojure functions so it doesn't help. On my machine, about 180 msecs To get unboxed primitives, you have to do more like what Parth did: > (defn dumb-test [] > (let [f2 (float 567.09723)] >(loop [f (float 1.2), i (long 1000)] > (if (zero? i) >f >(recur (/ f f2) (dec i)) On my machine that's 48 msecs. But we can do a bit better, just by using unchecked-dec: (defn dumb-test [] (let [f2 (float 567.09723)] (loop [f (float 1.2), i (long 1000)] (if (zero? i) f (recur (/ f f2) (unchecked-dec i)) That's 24 msecs for me. But I don't know how useful these kinds of micro-benchmarks really are. Clojure's "fast enough" so let's go solve some interesting problems... --Chouser --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: primitive meta data
On Oct 15, 8:34 am, Islon <[EMAIL PROTECTED]> wrote: > Hi. > > I've made a dumb (very dumb) performance comparison function just to play > with the language. > I wanted to mark some symbols with a float primitive type but the compiler > complained so I had to annotate it with the Float class. > Here is the function: > > (defn dumb-test [] > (let [#^Float f2 567.09723] > (loop [#^Float f 1.8, i 1000] > (if (zero? i) > f > (recur (/ f f2) (dec i)) > > And the test: > > (loop [i 50] > (time (dumb-test)) > (if (zero? i) > i > (recur (dec i > > There's a way to put a float primitive type in the metadata to prevent the > Float -> float unboxing? > Feel free to point some mistakes I probably made, I've just started learn > clojure. > > The clojure version took an average: "Elapsed time: 217.833342 msecs" in my > machine. The following was somewhat faster. (defn dumb-test [] (let [f2 (float 567.09723)] (loop [f (float 1.2), i (long 1000)] (if (zero? i) f (recur (/ f f2) (dec i)) I get about 59ms on my machine. user=> (time (dumb-test)) "Elapsed time: 59.245176 msecs" 0.0 Parth > > The java version (below) took average 23 msecs: > > private static float dumbTest() { > float f = 1.8f; > float f2 = 567.09723f; > for(int i = 0; i < 1000; i++) { > f /= f2; > } > return f; > } > > Regards. > Islon --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
primitive meta data
Hi. I've made a dumb (very dumb) performance comparison function just to play with the language. I wanted to mark some symbols with a float primitive type but the compiler complained so I had to annotate it with the Float class. Here is the function: (defn dumb-test [] (let [#^Float f2 567.09723] (loop [#^Float f 1.8, i 1000] (if (zero? i) f (recur (/ f f2) (dec i)) And the test: (loop [i 50] (time (dumb-test)) (if (zero? i) i (recur (dec i There's a way to put a float primitive type in the metadata to prevent the Float -> float unboxing? Feel free to point some mistakes I probably made, I've just started learn clojure. The clojure version took an average: "Elapsed time: 217.833342 msecs" in my machine. The java version (below) took average 23 msecs: private static float dumbTest() { float f = 1.8f; float f2 = 567.09723f; for(int i = 0; i < 1000; i++) { f /= f2; } return f; } Regards. Islon --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---