private static int countNumChars(String s) { int num = s.length(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { num--; } } return num; }
Is one of the fastest I've seen. It runs around 4366.293 On Dec 22, 1:43 pm, David Nolen <dnolen.li...@gmail.com> wrote: > On Wed, Dec 22, 2010 at 12:52 PM, Rayne <disciplera...@gmail.com> wrote: > > Running it gives me around 137343.295 nanoseconds. I've seen some Java > > algorithms that could run at just under 3000 nanoseconds. > > What do the Java implementations look like? > > (defn count-num-chars [^String s] > (let [l (.length s) > c (int \space)] > (loop [i 0 acc 0] > (if (< i l) > (recur (unchecked-inc-long i) > (if (= (int (.charAt s i)) c) acc > (unchecked-inc-long acc))) > acc)))) > > On 1.3.0 alpha3 on a 2.66ghz Core i7, 64bit OS X JDK 1.6 I see anywhere > from- > > 6900ns-11000ns > > Using identical?, codePointAt all make things slower for me. > > David -- 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