When using time a as a reference, run the tests many times to get an accurate result.
user> (defn example2 [] (let [r (range 1e7) a (take-while #(< % 12) r) b (drop-while #(< % 12) r)] [(count a) (count b)])) #'user/example2 user> (dorun (take 1000 (repeatedly example2))) nil user> (time (example2)) "Elapsed time: 614.4 msecs" [12 9999988]
I blame variance at runtime because the hotspot compiler has not yet fully opposed the classes created. Several times I ran the first and second examples and got mixed relative results:
run the example twice:
autotestbed.core> (time (let [r (range 1e7) a (take-while #(< % 12) r) b (drop-while #(< % 12) r)] [(count a) (count b)])) "Elapsed time: 929.681423 msecs" [12 9999988] autotestbed.core> (time (let [r (range 1e7) a (take-while #(< % 12) r) b (drop-while #(< % 12) r)] [(count a) (count b)])) "Elapsed time: 887.81269 msecs" [12 9999988]
then run the example two times:
core> (time (let [r (range 1e7) a (take-while #(< % 12) r) b (drop-while #(< % 12) r)] [(count a) (count b)])) "Elapsed time: 3838.751561 msecs" [12 9999988] core> (time (let [r (range 1e7) a (take-while #(< % 12) r) b (drop-while #(< % 12) r)] [(count a) (count b)])) "Elapsed time: 970.397078 msecs" [12 9999988]
sometiems, second example just as fast
source share