Created
November 27, 2012 06:50
-
-
Save dougc84/4152800 to your computer and use it in GitHub Desktop.
Array Benchmarking Tests
This file contains 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
#!/usr/bin/env ruby | |
require 'benchmark' | |
require 'active_support/core_ext/array' # required for Array.wrap. | |
# by the way, did you know there is a "my_array.fourty_two()" method? | |
Benchmark.bm do |b| | |
language = [] | |
100.times do | |
language << "8foanin" | |
end | |
times_for_test = 1_000_000 | |
b.report("Array.wrap with map") do | |
times_for_test.times do | |
employment_conditions = Array.wrap(language).map{ |option| "resumes.#{option} = true" } | |
end | |
end | |
b.report("Array.wrap with push") do | |
times_for_test.times do | |
employment_conditions = [] | |
Array.wrap(language).each do |option| | |
employment_conditions.push("resumes.#{option} = true") | |
end | |
end | |
end | |
b.report("Array.wrap with <<") do | |
times_for_test.times do | |
employment_conditions = [] | |
Array.wrap(language).each do |option| | |
employment_conditions << "resumes.#{option} = true" | |
end | |
end | |
end | |
b.report("Array() with map") do | |
times_for_test.times do | |
employment_conditions = Array(language).map{ |option| "resumes.#{option} = true" } | |
end | |
end | |
b.report("Array() with push") do | |
times_for_test.times do | |
employment_conditions = [] | |
Array(language).each do |option| | |
employment_conditions.push("resumes.#{option} = true") | |
end | |
end | |
end | |
b.report("Array() with <<") do | |
times_for_test.times do | |
employment_conditions = [] | |
Array(language).each do |option| | |
employment_conditions << "resumes.#{option} = true" | |
end | |
end | |
end | |
end | |
## results: | |
# running on: | |
# Intel(R) Core(TM) i7-2720QM CPU @ 2.20GHz | |
# 8 GB RAM | |
# | |
## notes: | |
# looks like .push is consistently slower throughout, with the one | |
# exception of when nil is passed in - it's pretty on par with the | |
# rest. i guess i won't be using .push anymore. | |
# | |
# Array() wins every time, unless language = nil, strangely. | |
# | |
# however, in the source code for Array.wrap, | |
# Array.wrap does a nil? check on the value(s) beforehand, | |
# before taking on the expense of casting it to another object type, | |
# so that's nice. | |
## when setting language to ["one", "two", "three"] at 10m iterations: | |
# | |
# user system total real | |
# Array.wrap with map 17.500000 0.020000 17.520000 ( 17.520294) | |
# Array.wrap with push 19.350000 0.020000 19.370000 ( 19.372810) | |
# Array.wrap with << 17.900000 0.040000 17.940000 ( 17.932323) | |
# Array() with map 15.960000 0.000000 15.960000 ( 15.966778) # RUNNER UP Array() with map | |
# Array() with push 17.290000 0.020000 17.310000 ( 17.303593) | |
# Array() with << 15.870000 0.020000 15.890000 ( 15.886304) # WINNER Array() with << | |
## when setting language to "one" at 10m iterations: | |
# | |
# user system total real | |
# Array.wrap with map 10.350000 0.040000 10.390000 ( 10.392860) | |
# Array.wrap with push 11.440000 0.070000 11.510000 ( 11.509545) | |
# Array.wrap with << 10.510000 0.000000 10.510000 ( 10.511079) | |
# Array() with map 9.590000 0.000000 9.590000 ( 9.596997) # WINNER Array() with map | |
# Array() with push 10.190000 0.010000 10.200000 ( 10.186085) | |
# Array() with << 9.860000 0.010000 9.870000 ( 9.880790) # RUNNER UP Array() with << | |
## when setting language to nil at 10m iterations: | |
# | |
# user system total real | |
# Array.wrap with map 3.840000 0.010000 3.850000 ( 3.857646) # RUNNER UP Array.wrap with map | |
# Array.wrap with push 3.840000 0.000000 3.840000 ( 3.843238) # RUNNER UP Array.wrap with push | |
# Array.wrap with << 3.840000 0.010000 3.850000 ( 3.837715) # WINNER Array.wrap with << | |
# Array() with map 5.230000 0.000000 5.230000 ( 5.236243) | |
# Array() with push 5.000000 0.000000 5.000000 ( 4.999214) | |
# Array() with << 4.960000 0.040000 5.000000 ( 5.002691) | |
## and for fun, when setting language to have 100 elements (only 1m iterations): | |
# | |
# user system total real | |
# Array.wrap with map 41.310000 0.110000 41.420000 ( 41.423694) | |
# Array.wrap with push 47.740000 0.080000 47.820000 ( 47.805172) | |
# Array.wrap with << 43.500000 0.070000 43.570000 ( 43.578899) # RUNNER UP Array.wrap with << | |
# Array() with map 41.210000 0.150000 41.360000 ( 41.358378) # WINNER Array() with map | |
# Array() with push 47.480000 0.220000 47.700000 ( 47.703488) | |
# Array() with << 42.960000 0.300000 43.260000 ( 43.256884) # RUNNER UP Array() with << |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment