Skip to content

Instantly share code, notes, and snippets.

@docwhat
Last active August 29, 2015 14:06
Show Gist options
  • Save docwhat/582fbb8c2c4186023b8c to your computer and use it in GitHub Desktop.
Save docwhat/582fbb8c2c4186023b8c to your computer and use it in GitHub Desktop.
require 'benchmark'
hash = {}
(1..5_000_000)
.map { |x| format '%020d', x }
.each { |k| hash["header#{k}"] = "value#{k.reverse}" }
Benchmark.bm(50) do |x|
puts "RUBY: #{RUBY_VERSION}"
x.report('map w/split join') { hash.map { |k, v| [k, v].join(':') } }
x.report('map w/split interpolation') { hash.map { |k, v| "#{k}:#{v}" } }
x.report('map w/split string cat +') { hash.map { |k, v| k + ':' + v } }
x.report('map w/split string cat <<') { hash.map { |k, v| '' << k << ':' << v } }
x.report('map join') { hash.map { |m| m.join(':') } }
x.report('map interpolation') { hash.map { |m| "#{m.first}:#{m.last}" } }
x.report('map string cat +') { hash.map { |m| m.first + ':' + m.last } }
x.report('map string cat <<') { hash.map { |m| '' << m.first << ':' << m.last } }
end
$ for rv in 1.9.2 1.9.3 2.0.0 2.1.2; do rbenv shell "$rv" && ruby hash-mark.rb; done ; rbenv shell --unset
user system total real
RUBY: 1.9.2
map w/split join 8.690000 0.380000 9.070000 ( 9.257528)
map w/split interpolation 8.860000 0.190000 9.050000 ( 9.102149)
map w/split string cat + 7.510000 0.240000 7.750000 ( 7.810475)
map w/split string cat << 8.200000 0.200000 8.400000 ( 8.378744)
map join 6.820000 0.300000 7.120000 ( 7.119914)
map interpolation 7.880000 0.200000 8.080000 ( 8.081020)
map string cat + 6.730000 0.180000 6.910000 ( 6.900275)
map string cat << 8.260000 0.270000 8.530000 ( 8.508148)
user system total real
RUBY: 1.9.3
map w/split join 10.990000 0.330000 11.320000 ( 11.324523)
map w/split interpolation 9.880000 0.170000 10.050000 ( 10.053683)
map w/split string cat + 8.860000 0.150000 9.010000 ( 8.998289)
map w/split string cat << 11.660000 0.180000 11.840000 ( 11.847003)
map join 9.790000 0.220000 10.010000 ( 10.002983)
map interpolation 9.790000 0.130000 9.920000 ( 9.932695)
map string cat + 8.370000 0.090000 8.460000 ( 8.445293)
map string cat << 11.420000 0.140000 11.560000 ( 11.569196)
user system total real
RUBY: 2.0.0
map w/split join 12.210000 0.330000 12.540000 ( 12.547461)
map w/split interpolation 7.800000 0.180000 7.980000 ( 8.027406)
map w/split string cat + 7.200000 0.180000 7.380000 ( 7.461249)
map w/split string cat << 9.730000 0.140000 9.870000 ( 9.865895)
map join 8.770000 0.230000 9.000000 ( 9.184187)
map interpolation 8.170000 0.150000 8.320000 ( 8.398066)
map string cat + 8.790000 0.080000 8.870000 ( 8.876930)
map string cat << 10.480000 0.090000 10.570000 ( 10.580655)
user system total real
RUBY: 2.1.2
map w/split join 16.010000 0.340000 16.350000 ( 16.350871)
map w/split interpolation 9.770000 0.170000 9.940000 ( 9.943556)
map w/split string cat + 10.760000 0.120000 10.880000 ( 10.879686)
map w/split string cat << 12.410000 0.180000 12.590000 ( 12.586148)
map join 12.900000 0.260000 13.160000 ( 13.176739)
map interpolation 9.870000 0.150000 10.020000 ( 10.015435)
map string cat + 11.140000 0.080000 11.220000 ( 11.232823)
map string cat << 12.670000 0.160000 12.830000 ( 12.824191)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment