hraberg / shen.clj

神.clj | Shen for Clojure. Shen is a portable functional programming language by Mark Tarver.
shenlanguage.org
Other
155 stars 9 forks source link

cond/case & ^:const #1

Closed AlexBaranosky closed 11 years ago

AlexBaranosky commented 11 years ago
user=> (time (dotimes [i 100000] (condp = i 1 2 3 4 5 6 7 8 9)))
"Elapsed time: 36.561 msecs"
nil
user=> (time (dotimes [i 100000] (case i 1 2 3 4 5 6 7 8 9)))
"Elapsed time: 2.331 msecs"
nil
user=> (time (dotimes [i 100000] (cond (= i 1) 2 (= i 3) 4  (= i 5) 6  (= i 7) 8 :else 9)))
"Elapsed time: 2.309 msecs"
AlexBaranosky commented 11 years ago

This macro gives you the same effect as (some [foo] ....) but much faster, since it expands to a cond.

(defmacro pred-cond 
  "Checks each predicate against the item, returning the corresponding 
   result if it finds a match, otherwise returning nil.
   Assumes item to be a value, as it will get evaluated multiple times."
  [item pred result & preds+results]
  (cond (= pred :else ) result
        (not (seq preds+results)) `(if (~pred ~item) ~result nil) ;; last condition, but no :else in the form
        :else `(if (~pred ~item)
                 ~result
                 (pred-cond ~item ~@preds+results))))
#'user/pred-cond
user=> (time (dotimes [i 100000] (pred-cond "a" string? true :else false)))
"Elapsed time: 2.504 msecs"
nil
hraberg commented 11 years ago

This actually introduced a bug in boolean? - just pushed a fix.

When running ./build, it's easy to focus on the run time, and forget the actual test output - both these numbers should be as close to 0 as possible :-)

passed ... 146
failed ...0
pass rate ...100%

ok
0

run time: 17.895 secs
AlexBaranosky commented 11 years ago

I was, apparently, running the wrong script to test, sorry. On Jan 23, 2013 4:21 AM, "Håkan Råberg" notifications@github.com wrote:

This actually introduced a bug in boolean? - just pushed a fix.

When running ./build, it's easy to focus on the run time, and forget the actual test output - both these numbers should be as close to 0 as possible :-)

passed ... 146 failed ...0 pass rate ...100%

ok 0

run time: 17.895 secs

— Reply to this email directly or view it on GitHubhttps://github.com/hraberg/shen.clj/pull/1#issuecomment-12593162.

AlexBaranosky commented 11 years ago

Wow, the build went (very roughly) 17.5% faster with these changes!

On Wed, Jan 23, 2013 at 9:04 AM, Alex Baranosky < alexander.baranosky@gmail.com> wrote:

I was, apparently, running the wrong script to test, sorry. On Jan 23, 2013 4:21 AM, "Håkan Råberg" notifications@github.com wrote:

This actually introduced a bug in boolean? - just pushed a fix.

When running ./build, it's easy to focus on the run time, and forget the actual test output - both these numbers should be as close to 0 as possible :-)

passed ... 146 failed ...0 pass rate ...100%

ok 0

run time: 17.895 secs

— Reply to this email directly or view it on GitHubhttps://github.com/hraberg/shen.clj/pull/1#issuecomment-12593162.

hraberg commented 11 years ago

Yep, Thanks! I think there's further speed gains to be found for sure..