Skip to content

Instantly share code, notes, and snippets.

@asterite
Created April 28, 2015 21:51
Show Gist options
  • Save asterite/d4e3848629969f5872df to your computer and use it in GitHub Desktop.
Save asterite/d4e3848629969f5872df to your computer and use it in GitHub Desktop.
require "benchmark"
RANGE = (1..10_000_000)
TIMES = 100_000
a1 = nil
a2 = nil
a3 = nil
Benchmark.bm do |x|
x.report("regular") do
# TIMES.times do
a3 = RANGE.map { |x| x * 2 }.select { |x| 100 <= x <= 200 }.take(10)
# end
end
x.report("manual") do
TIMES.times do
temp = [] of Int32
count = 0
RANGE.each do |x|
x *= 2
if 100 <= x <= 200
temp << x
count += 1
break if count == 10
end
end
a1 = temp
end
end
x.report("iterator") do
TIMES.times do
a2 = RANGE.each.map { |x| x * 2 }.select { |x| 100 <= x <= 200 }.take(10).to_a
end
end
end
pp a1 == a2
pp a1 == a3
# pp a2 == a3
user system total real
regular 0.070000 0.030000 0.100000 ( 0.078614)
manual 0.020000 0.000000 0.020000 ( 0.021547)
iterator 0.050000 0.000000 0.050000 ( 0.054832)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment