Skip to content

Instantly share code, notes, and snippets.

@jodosha
Last active August 29, 2015 14:12
Show Gist options
  • Save jodosha/5c769a5e2f7b27c8b846 to your computer and use it in GitHub Desktop.
Save jodosha/5c769a5e2f7b27c8b846 to your computer and use it in GitHub Desktop.
Lotus::Entity#initialize bench: metaprogramming vs iterative
#!/usr/bin/env ruby
require 'rubygems'
require 'benchmark/ips'
require 'lotus/utils/attributes'
class MetaProgrammingEntity
def initialize(attributes = {})
attributes = Lotus::Utils::Attributes.new(attributes)
@a = attributes.get(:a)
@b = attributes.get(:b)
@c = attributes.get(:c)
@d = attributes.get(:a)
@e = attributes.get(:e)
@f = attributes.get(:f)
@g = attributes.get(:g)
@h = attributes.get(:h)
@i = attributes.get(:i)
@j = attributes.get(:j)
end
end
class IterativeEntity
attr_writer :a, :b, :c, :d, :e, :f, :g, :h, :i, :j
def initialize(attributes = {})
attributes.each do |k, v|
setter = "#{ k }="
public_send(setter, v) if respond_to?(setter)
end
end
end
ATTRIBUTES = { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5,
'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10 }
Benchmark.ips do |x|
x.report('meta #initialize') { MetaProgrammingEntity.new(ATTRIBUTES) }
x.report('iter #initialize') { IterativeEntity.new(ATTRIBUTES) }
end
__END__
Result:
Calculating -------------------------------------
meta #initialize 7.437k i/100ms
iter #initialize 11.623k i/100ms
-------------------------------------------------
meta #initialize 84.850k (± 5.7%) i/s - 423.909k
iter #initialize 137.717k (± 6.9%) i/s - 685.757k
Ruby:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
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: 17 days 22:44
#!/usr/bin/env ruby
require 'rubygems'
require 'benchmark/ips'
require 'lotus/utils/attributes'
class SymbolEntity
def initialize(attributes = {})
@a = attributes[:a]
@b = attributes[:b]
@c = attributes[:c]
@d = attributes[:a]
@e = attributes[:e]
@f = attributes[:f]
@g = attributes[:g]
@h = attributes[:h]
@i = attributes[:i]
@j = attributes[:j]
end
end
class IterativeEntity
attr_writer :a, :b, :c, :d, :e, :f, :g, :h, :i, :j
def initialize(attributes = {})
attributes.each do |k, v|
setter = "#{ k }="
public_send(setter, v) if respond_to?(setter)
end
end
end
ATTRIBUTES = { a: 1, b: 2, c: 3, d: 4, e: 5,
f: 6, g: 7, h: 8, i: 9, j: 10 }
Benchmark.ips do |x|
x.report('symbol #initialize') { SymbolEntity.new(ATTRIBUTES) }
x.report('iter #initialize') { IterativeEntity.new(ATTRIBUTES) }
end
__END__
Calculating -------------------------------------
symbol #initialize 42.997k i/100ms
iter #initialize 9.567k i/100ms
-------------------------------------------------
symbol #initialize 736.743k (±11.5%) i/s - 3.655M
iter #initialize 113.413k (± 7.8%) i/s - 564.453k
Ruby:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
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: 17 days 22:44
#!/usr/bin/env ruby
require 'rubygems'
require 'benchmark/ips'
require 'lotus/utils/hash'
class SymbolizedEntity
def initialize(attributes = {})
attributes = Lotus::Utils::Hash.new(attributes).symbolize!
@a = attributes[:a]
@b = attributes[:b]
@c = attributes[:c]
@d = attributes[:a]
@e = attributes[:e]
@f = attributes[:f]
@g = attributes[:g]
@h = attributes[:h]
@i = attributes[:i]
@j = attributes[:j]
end
end
class IterativeEntity
attr_writer :a, :b, :c, :d, :e, :f, :g, :h, :i, :j
def initialize(attributes = {})
attributes.each do |k, v|
setter = "#{ k }="
public_send(setter, v) if respond_to?(setter)
end
end
end
ATTRIBUTES = { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5,
'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10 }
Benchmark.ips do |x|
x.report('symbolized #initialize') { SymbolizedEntity.new(ATTRIBUTES) }
x.report('iter #initialize') { IterativeEntity.new(ATTRIBUTES) }
end
__END__
Calculating -------------------------------------
symbolized #initialize
10.291k i/100ms
iter #initialize 9.574k i/100ms
-------------------------------------------------
symbolized #initialize
116.616k (± 5.3%) i/s - 586.587k
iter #initialize 108.023k (±12.4%) i/s - 536.144k
Ruby:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
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: 17 days 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment