Created
September 16, 2017 05:59
-
-
Save kaityo256/081d096b78f020c63a54b2ce6d67c46d to your computer and use it in GitHub Desktop.
Initializing array by push or block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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