Skip to content

Instantly share code, notes, and snippets.

@JacobNinja
Last active December 14, 2015 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobNinja/5104046 to your computer and use it in GitHub Desktop.
Save JacobNinja/5104046 to your computer and use it in GitHub Desktop.
Initial implementation of Reducers in Ruby
def fib(n)
if (n < 2)
n
else
fib(n-1) + fib(n-2)
end
end
JRuby 1.7.2
irb(main):078:0> Benchmark.measure { Reducer.new(1..40).map {|i| fib(i) }.map {|i| i * 2}.reduce(:+) }
=> #<Benchmark::Tms:0x3d63b102 @stime=0.06999999999999851, @real=21.506999969482422, @total=36.930000000000014, @cutime=0.0, @utime=36.860000000000014, @label="", @cstime=0.0>
irb(main):079:0> Benchmark.measure { (1..40).map {|i| fib(i) }.map {|i| i * 2}.reduce(:+) }
=> #<Benchmark::Tms:0x22013e9b @stime=0.03999999999999915, @real=31.621999979019165, @total=31.640000000000022, @cutime=0.0, @utime=31.600000000000023, @label="", @cstime=0.0>
JRuby 1.7.3 (more threads)
irb(main):010:0> Benchmark.measure { Reducer.new(1..40).map {|i| fib(i) }.map {|i| i * 2}.reduce(:+) }
=> #<Benchmark::Tms:0x7756c69c @stime=0.1299999999999999, @real=9.821999788284302, @total=29.94, @cutime=0.0, @utime=29.810000000000002, @label="", @cstime=0.0>
irb(main):011:0> Benchmark.measure { (1..40).map {|i| fib(i) }.map {|i| i * 2}.reduce(:+) }
=> #<Benchmark::Tms:0x7cd76237 @stime=0.030000000000000027, @real=25.450000047683716, @total=25.450000000000003, @cutime=0.0, @utime=25.42, @label="", @cstime=0.0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment