Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created September 16, 2017 05:59
Show Gist options
  • Save kaityo256/081d096b78f020c63a54b2ce6d67c46d to your computer and use it in GitHub Desktop.
Save kaityo256/081d096b78f020c63a54b2ce6d67c46d to your computer and use it in GitHub Desktop.
Initializing array by push or block
require 'benchmark'
def use_push(size,trial)
trial.times do
a = []
size.times do |i|
a.push i
end
end
end
def use_block(size,trial)
trial.times do
a = Array.new(size){|i| i}
end
end
SIZE=1000
TRIAL=10000
r1 = Benchmark.realtime do
use_push(SIZE,TRIAL)
end
r2 = Benchmark.realtime do
use_block(SIZE,TRIAL)
end
puts "push #{r1} [sec]" # => push 0.6004580000007991 [sec]
puts "block #{r2} [sec]" # => block 0.3890559999999823 [sec]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment