Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Created February 6, 2013 14:56
Show Gist options
  • Save rosenfeld/4723061 to your computer and use it in GitHub Desktop.
Save rosenfeld/4723061 to your computer and use it in GitHub Desktop.
Performance comparison: strings vs symbols in Ruby
ruby symbols-performance.rb
Rehearsal ------------------------------------
0.540000 0.010000 0.550000 ( 0.543421)
0.680000 0.020000 0.700000 ( 0.705931)
--------------------------- total: 1.250000sec
user system total real
0.240000 0.000000 0.240000 ( 0.244554)
0.380000 0.000000 0.380000 ( 0.391517)
require 'benchmark'
hashes = []
1_000_000.times { hashes << { some_key_name: 1, 'some_key_name' => 2 }}
Benchmark.bmbm do |x|
x.report { hashes.map{|h| h[:some_key_name]} }
x.report { hashes.map{|h| h['some_key_name']} }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment