require 'benchmark'
Benchmark.bm { |x|
x.report('[*..] ') do
[*1000000 .. 9999999]
end
x.report('(..).to_a') do
(1000000 .. 9999999).to_a
end
x.report('Array(..)') do
Array(1000000 .. 9999999)
end
x.report('Array.new(n, &:next)') do
Array.new(8999999, &:next)
end
}
Be careful, this tricky method Array.new(n, &:next) is slower while three other basic methods are same.
user system total real
[*..] 0.734000 0.110000 0.844000 ( 0.843753)
(..).to_a 0.703000 0.062000 0.765000 ( 0.843752)
Array(..) 0.750000 0.016000 0.766000 ( 0.859374)
Array.new(n, &:next) 1.250000 0.000000 1.250000 ( 1.250002)