On Sunday, 2 March 2014 05:32:00 UTC+5:30, bob wrote:
>
>
> Good point, Thanks a lot. 
>
> Shall we improve the str fn in the core lib? From my point of view, the 
> core fns should be performance sensitive.
>

If string formation is the bottleneck in your app and if you can come up 
with a version of `str` function that works in all use-cases, then you can 
probably `alter-var-root` the str fn with yours as long as you own the 
responsibility.

I noticed the following macro (ignore the reflection warnings) can help 
shave some nanoseconds in a large tight loop, but I leave to you to decide 
how much worth it really is:

(defmacro sb-str
  [& args]
  (cond (empty? args)      ""
        (= 1 (count args)) (let [x (first args)]
                             `(let [y# ~x]
                                (cond (nil? y#)                ""
                                      (instance? Boolean   y#) (.toString 
(Boolean.   y#))
                                      (instance? Byte      y#) (.toString 
(Byte.      y#))
                                      (instance? Character y#) (.toString 
(Character. y#))
                                      (instance? Double    y#) (.toString 
(Double.    y#))
                                      (instance? Float     y#) (.toString 
(Float.     y#))
                                      (instance? Integer   y#) (.toString 
(Integer.   y#))
                                      (instance? Long      y#) (.toString 
(Long.      y#))
                                      (instance? Short     y#) (.toString 
(Short.     y#))
                                      :otherwise               (.toString 
y#))))
        :otherwise         (let [sb (gensym)
                                each-append #(list '.append sb %)
                                all-appends (map each-append args)]
                            `(let [~sb (StringBuilder.)]
                               ~@all-appends
                               (.toString ~sb)))))

Note that it is not a function, so you cannot use it with high order 
functions. You can possibly use `definline` instead of a macro but you lose 
varargs then.

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.

Reply via email to