Skip to content

Instantly share code, notes, and snippets.

@diegodurs
Created June 16, 2015 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegodurs/e7067a14a8c1da317b14 to your computer and use it in GitHub Desktop.
Save diegodurs/e7067a14a8c1da317b14 to your computer and use it in GitHub Desktop.
Benchmark with_indifferent_access & symbolize_keys Hash
require 'active_support/core_ext/hash'
require 'benchmark'
Benchmark.bm do |x|
x.report("string") do
1_000.times do
data_str = {
"id" => "06e99a1b-4020-4380-ab27-1a3e0c5e557c",
"type" => "Person",
"score" => "100",
"name" => "Nicolas Jaar",
"sort-name" => "Jaar, Nicolas",
"gender" => "male",
"country" => "US",
"area" => {"id" => "489ce91b-6658-3307-9877-795b68554c98","name" => "United States","sort-name" => "United States"},
"life-span" => {"begin" => "1990-01-10","ended" => nil},
"tags" => [
{"count" => 1,"name" => "electronic"},
{"count" => 1,"name" => "house"},
{"count" => 1,"name" => "minimal"}
]
}
end
end
x.report('symbol') do
1_000.times do
data_str = {
"id" => "06e99a1b-4020-4380-ab27-1a3e0c5e557c",
"type" => "Person",
"score" => "100",
"name" => "Nicolas Jaar",
"sort-name" => "Jaar, Nicolas",
"gender" => "male",
"country" => "US",
"area" => {"id" => "489ce91b-6658-3307-9877-795b68554c98","name" => "United States","sort-name" => "United States"},
"life-span" => {"begin" => "1990-01-10","ended" => nil},
"tags" => [
{"count" => 1,"name" => "electronic"},
{"count" => 1,"name" => "house"},
{"count" => 1,"name" => "minimal"}
]
}
# data_sym = data_str.with_indifferent_access
data_sym = data_str.deep_symbolize_keys
end
end
end
# user system total real
# string 0.030000 0.000000 0.030000 ( 0.022233)
# symbol 0.060000 0.000000 0.060000 ( 0.063261)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment