Skip to content

Instantly share code, notes, and snippets.

@sickill
Last active Feb 8, 2017
Embed
What would you like to do?
Naive comparison of reduce/sum performance of Clojure 1.8, ClojureScipt (Planck 2.0, Lumo 1.1.0), Ruby 2.4, JRuby 9.1.7.0 and Elixir 1.3.2

Naive micro-benchmark of reduce/sum I made to get the idea of raw performance range of different dynamic languages / different runtimes.

For each of these I repeated the execution until the timing stopped changing in major way (JIT etc), and saved only the last run.

Don't take it too seriously ;)

irb(main):032:0> time_method { Range.new(0, 10000000).reduce(0, &:+) }
Time elapsed 1126.0 milliseconds
irb(main):027:0> time_method { Range.new(0, 10000000).reduce(0) { |acc, n| acc + n } }
Time elapsed 655.0 milliseconds
cljs.user=> (time (reduce + 0 (range 10000000)))
"Elapsed time: 1319.600832 msecs"
cljs.user=> (time (reduce #(+ %1 %2) 0 (range 10000000)))
"Elapsed time: 1341.649197 msecs"
irb(main):021:0> time_method { Range.new(0, 10000000).reduce(0, &:+) }
Time elapsed 545.2099999999999 milliseconds
irb(main):018:0> time_method { Range.new(0, 10000000).reduce(0) { |acc, n| acc + n } }
Time elapsed 675.584 milliseconds
cljs.user=> (time (reduce + 0 (range 10000000)))
"Elapsed time: 46.766405 msecs"
cljs.user=> (time (reduce #(+ %1 %2) 0 (range 10000000)))
"Elapsed time: 77.217126 msecs"
boot.user=> (time (reduce + 0 (range 10000000)))
"Elapsed time: 71.780456 msecs"
boot.user=> (time (reduce #(+ %1 %2) 0 (range 10000000)))
"Elapsed time: 131.262269 msecs"
iex(7)> Benchmark.measure(fn -> Enum.reduce(Range.new(0, 10000000), 0, &Kernel.+/2) end)
0.652629s
iex(6)> Benchmark.measure(fn -> Enum.reduce(Range.new(0, 10000000), 0, fn acc, n -> acc + n end) end)
11.392735s
# note: when I ran these via `elixir reduce.ex` it got compiled and both numbers were around 0.6s.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment