Last active
August 29, 2015 14:10
-
-
Save jodosha/a35b8e6d25e5d3742d0e to your computer and use it in GitHub Desktop.
Lotus::Utils::Attributes: benchmark String vs Symbol as keys
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/ips' | |
require 'allocation_stats' | |
require_relative './lib/lotus/utils/hash' | |
require_relative './lib/lotus/utils/attributes' | |
require_relative './lib/lotus/utils/attributes_sym' | |
TIMES = 1_000 | |
RAW = Hash[(1..100).map {|i| [i.to_s * 10, i]}] | |
string = Lotus::Utils::Attributes.new(RAW) | |
symbol = Lotus::Utils::AttributesSym.new(RAW) | |
key_string = '100'.freeze | |
key_symbol = key_string.to_sym.freeze | |
puts "BENCHMARK:\n\n" | |
Benchmark.ips do |x| | |
x.report('#initialize (string)') { Lotus::Utils::Attributes.new(RAW) } | |
x.report('#initialize (symbol)') { Lotus::Utils::AttributesSym.new(RAW) } | |
x.report('#get (string w/ string)') { string.get(key_string) } | |
x.report('#get (string w/ sym)' ) { string.get(key_symbol) } | |
x.report('#get (symbol w/ string)') { symbol.get(key_string) } | |
x.report('#get (symbol w/ sym)' ) { symbol.get(key_symbol) } | |
end | |
puts "\nOBJECTS ALLOCATIONS:\n\n" | |
def trace_allocations(label, &blk) | |
stats = AllocationStats.trace do | |
TIMES.times(&blk) | |
end | |
puts "Allocations for: #{ label }" | |
puts stats.allocations(alias_paths: true).group_by(:sourcefile, :sourceline, :class).to_text | |
end | |
trace_allocations '#initialize (string)' do | |
Lotus::Utils::Attributes.new(RAW) | |
end | |
trace_allocations '#initialize (symbol)' do | |
Lotus::Utils::AttributesSym.new(RAW) | |
end | |
trace_allocations '#get (string w/ string)' do | |
string.get(key_string) | |
end | |
trace_allocations '#get (string w/ sym)' do | |
string.get(key_symbol) | |
end | |
trace_allocations '#get (symbol w/ string)' do | |
symbol.get(key_string) | |
end | |
trace_allocations '#get (symbol w/ sym)' do | |
symbol.get(key_symbol) | |
end | |
__END__ | |
Results: | |
BENCHMARK: | |
Calculating ------------------------------------- | |
#initialize (string) 657.000 i/100ms | |
#initialize (symbol) 771.000 i/100ms | |
#get (string w/ string) 41.014k i/100ms | |
#get (string w/ sym) 33.976k i/100ms | |
#get (symbol w/ string) 37.679k i/100ms | |
#get (symbol w/ sym) 41.791k i/100ms | |
------------------------------------------------- | |
#initialize (string) 6.764k (± 5.7%) i/s - 34.164k | |
#initialize (symbol) 7.870k (± 6.4%) i/s - 39.321k | |
#get (string w/ string) 1.575M (±10.8%) i/s - 7.793M | |
#get (string w/ sym) 1.258M (±13.1%) i/s - 6.150M | |
#get (symbol w/ string) 1.433M (± 7.3%) i/s - 7.159M | |
#get (symbol w/ sym) 1.672M (± 7.9%) i/s - 8.316M | |
OBJECTS ALLOCATIONS: | |
Allocations for: #initialize (string) | |
sourcefile sourceline class count | |
----------------------------------------------- ---------- ------------------------ ----- | |
<PWD>/lib/lotus/utils/hash.rb 79 String 100 | |
<GEM:lotus-utils-0.3.1>/lib/lotus/utils/hash.rb 174 String 99 | |
<GEM:lotus-utils-0.3.1>/lib/lotus/utils/hash.rb 135 Array 1000 | |
<PWD>/lib/lotus/utils/attributes.rb 33 Lotus::Utils::Hash 1000 | |
attributes_bench.rb 41 Lotus::Utils::Attributes 1000 | |
Allocations for: #initialize (symbol) | |
sourcefile sourceline class count | |
----------------------------------------------- ---------- --------------------------- ----- | |
<GEM:lotus-utils-0.3.1>/lib/lotus/utils/hash.rb 135 Array 1000 | |
<PWD>/lib/lotus/utils/attributes_sym.rb 33 Lotus::Utils::Hash 1000 | |
attributes_bench.rb 45 Lotus::Utils::AttributesSym 1000 | |
Allocations for: #get (string w/ string) | |
sourcefile sourceline class count | |
---------- ---------- ----- ----- | |
Allocations for: #get (string w/ sym) | |
sourcefile sourceline class count | |
----------------------------------- ---------- ------ ----- | |
<PWD>/lib/lotus/utils/attributes.rb 61 String 1000 | |
Allocations for: #get (symbol w/ string) | |
sourcefile sourceline class count | |
---------- ---------- ----- ----- | |
Allocations for: #get (symbol w/ sym) | |
sourcefile sourceline class count | |
---------- ---------- ----- ----- | |
Ruby: | |
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0] | |
Hardware: | |
Hardware Overview: | |
Model Name: MacBook Air | |
Model Identifier: MacBookAir5,2 | |
Processor Name: Intel Core i7 | |
Processor Speed: 2 GHz | |
Number of Processors: 1 | |
Total Number of Cores: 2 | |
L2 Cache (per Core): 256 KB | |
L3 Cache: 4 MB | |
Memory: 8 GB | |
Boot ROM Version: MBA51.00EF.B02 | |
SMC Version (system): 2.5f9 | |
Software: | |
System Software Overview: | |
System Version: OS X 10.9.5 (13F34) | |
Kernel Version: Darwin 13.4.0 | |
Time since boot: 10 days 9:20 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment