On Saturday, 1 March 2014 15:32:41 UTC+5:30, bob wrote: > > Case : > > clojure verison: > > (time (dotimes [n 10000000] (str n "another word"))) ;; take about 5000msec > > java version > > long time = System.nanoTime(); > > for(int i=0 ; i<10000000 ;i++){ > String a=i+"another word"; > } > System.out.println(System.nanoTime()-time); > > > The java version take about 500 msecs, I thought it might be caused by the > str implementation which is using string builder, and it might not be the > best choice in the case of no much string to concat, and then I replace > "another word" with 5 long strings as the parameter, however no surprise. > > I just wonder what make the difference, or how to find the difference. >
Others have added useful points to this thread. Java string concatenation internally uses StringBuilder, so if you replace (str n "another word") with the following: (let [sb (StringBuilder.)] (.append sb n) (.append sb "another word") (.toString sb)) ..then the perf improves 1/4 to 1/3. Further, with the following tweak: (let [sb (StringBuilder. 20)] ; because StringBuilder allocates only 16 chars by default on Oracle JRE (.append sb n) (.append sb "another word") (.toString sb)) ..the perf improves from 1/3 to less than 1/2. Here we simply avoid double allocation in StringBuilder. Other things I made sure were: 1. I used Criterium to measure 2. I used `-server` option 3. Made sure reflection warning was on Shantanu -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.