Skip to content

Instantly share code, notes, and snippets.

@texpert
Created December 13, 2019 12:01
Show Gist options
  • Save texpert/db13f2a344c1151cc2b10ef03a083ed0 to your computer and use it in GitHub Desktop.
Save texpert/db13f2a344c1151cc2b10ef03a083ed0 to your computer and use it in GitHub Desktop.
/bin/bash -c "/home/thunder/.rvm/bin/rvm ruby-2.5.1 do /home/thunder/.rvm/rubies/ruby-2.5.1/bin/ruby /home/thunder/.RubyMine2019.3/config/scratches/scratch_1.rb"
Warming up --------------------------------------
Active Support symbolize_keys
37.892k i/100ms
Hash keys map to_sym 60.148k i/100ms
Calculating -------------------------------------
Active Support symbolize_keys
562.840k (± 1.4%) i/s - 5.608M in 10.047830s
Hash keys map to_sym 767.850k (± 1.1%) i/s - 7.699M in 10.048546s
with 95.0% confidence
Comparison:
Hash keys map to_sym: 767850.4 i/s
Active Support symbolize_keys: 562839.7 i/s - 1.36x (± 0.02) slower
with 95.0% confidence
Code:
_________
# frozen_string_literal: true
# gem install benchmark-ips
# gem install kalibera
# gem install active_support
require 'benchmark/ips'
require 'active_support'
require 'active_support/core_ext/hash'
# Enable and start GC before each job run. Disable GC afterwards.
#
# Inspired by https://www.omniref.com/ruby/2.2.1/symbols/Benchmark/bm?#annotation=4095926&line=182
class GCSuite
def warming(*)
run_gc
end
def running(*)
run_gc
end
def warmup_stats(*)
end
def add_report(*)
end
private
def run_gc
GC.enable
GC.start
GC.disable
end
end
suite = GCSuite.new
h = { 'a' => 1, b: 2}
Benchmark.ips do |x|
x.config(:time => 10, :warmup => 5, :stats => :bootstrap, :confidence => 95)
x.report('Active Support symbolize_keys') do
h.symbolize_keys
end
x.report('Hash keys map to_sym') do
h.keys.map { |k| k.is_a?(Symbol) ? k : k.to_sym }
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment