Thanks for the replies guys. Both solutions outperform the original. The one using unchecked operations is about 60x faster, which is the kind of performance I was hoping for. It's good to see that Clojure has the capacity to do things very quickly. I just need to learn all the tricks to make it do so. I'm going to use this code as part of a small agent demo I'm working on. Basically, it's going to take in work units (MD5'd strings) and run brute force comparisons on them until a match is found. I can fire up an agent per core and compare performance numbers as the workload is parallelized. It's really simple stuff but good for a concurrency beginner like me to explore with.
Thanks, Travis On Jun 14, 10:00 am, tmountain <tinymount...@gmail.com> wrote: > I've been playing around with rewriting some Java code in Clojure and > did some simple benchmarking in the process. In this case, there's a > huge disparity in the performance numbers between the two languages, > and I'm wondering what the cause may be. The program rotates a string > from "aaaa", "aaab", ..., "zzzz". The Java version takes 0.77 seconds > to complete while the Clojure version takes 22 seconds. I've tried to > make the scripts relatively isomorphic and have verified that they > produce the same results. I'm pasting the source below. > > tra...@travis-ubuntu:/tmp% time clj base26.clj > clj base26.clj 21.99s user 1.23s system 85% cpu 27.318 total > > tra...@travis-ubuntu:/tmp% time java Base26 > java Base26 0.77s user 0.04s system 78% cpu 1.029 total > > clojure version: > > (defn base26 [n] > (let [seed-string "aaaa" > s (new StringBuilder seed-string)] > (loop [pos (- (count seed-string) 1) > x n] > (if (> x 0) > (let [digit (+ (int \a) (mod x 26))] > (. s setCharAt pos (char digit)) > (if (and (> pos 0) (> x 0)) > (recur (- pos 1) (/ x 26)))))) > (. s toString))) > > (doseq [i (range (Math/pow 26 4))] > (base26 i)) > > java version: > > import java.lang.StringBuilder; > > public class Base26 { > public static void main(String[] args) { > for (int i = 0; i < Math.pow(26, 4); i++) { > Base26.base26(i); > } > } > > public static String base26(int num) { > if (num < 0) { > throw new IllegalArgumentException("Only positive numbers > are supported"); > } > StringBuilder s = new StringBuilder("aaaa"); > for (int pos = 3; pos >= 0 && num > 0 ; pos--) { > char digit = (char) ('a' + num % 26); > s.setCharAt(pos, digit); > num = num / 26; > } > return s.toString(); > } > > } > > I've tried warn-on-reflection, and it didn't report anything. > > Thanks, > Travis --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---