Skip to content

Instantly share code, notes, and snippets.

@phstc
Last active December 18, 2015 05:49
Show Gist options
  • Save phstc/5735975 to your computer and use it in GitHub Desktop.
Save phstc/5735975 to your computer and use it in GitHub Desktop.
=begin
~ ruby test.rb
n = 10000
user system total real
0.010000 0.000000 0.010000 ( 0.002855)
0.000000 0.000000 0.000000 ( 0.005343)
--
n = 20000
user system total real
0.010000 0.000000 0.010000 ( 0.005923)
0.010000 0.000000 0.010000 ( 0.009862)
--
n = 30000
user system total real
0.010000 0.000000 0.010000 ( 0.008921)
0.010000 0.000000 0.010000 ( 0.014062)
--
n = 40000
user system total real
0.010000 0.000000 0.010000 ( 0.013030)
0.020000 0.000000 0.020000 ( 0.018478)
--
n = 50000
user system total real
0.010000 0.000000 0.010000 ( 0.013829)
0.030000 0.000000 0.030000 ( 0.025704)
=end
require "benchmark"
c = ARGV[0] ? ARGV[0].to_i : 10000
(1..5).each do | nn |
n = c * nn
puts "n = #{n}"
Benchmark.bm do |x|
x.report {
(0..n).inject { |sum, i|
sum + ((i = i + 1) % 2 == 0 ? i - 1 : 0)
}
}
x.report {
(0..n).map { |i| i + 1 }.
select { |j| j % 2 == 0 }.
map { |k| k - 1 }.
reduce(&:+)
}
puts "--"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment