Skip to content

Instantly share code, notes, and snippets.

@Erol
Last active January 11, 2017 11:10
Show Gist options
  • Save Erol/d7a4389e0a1523b72576 to your computer and use it in GitHub Desktop.
Save Erol/d7a4389e0a1523b72576 to your computer and use it in GitHub Desktop.
Ethos Benchmark Comparison
require 'benchmark/ips'
require 'fast_attributes'
require 'virtus'
require 'attrio'
require 'anima'
require 'ethos/entity'
ATTR_NAMES = [:attr0, :attr1, :attr2, :attr3, :attr4, :attr5, :attr6, :attr7, :attr8, :attr9]
class FastIntegers
extend FastAttributes
define_attributes initialize: true do
attribute *ATTR_NAMES, Integer
end
end
class AttrioIntegers
include Attrio
define_attributes do
ATTR_NAMES.each do |name|
attr name, Integer
end
end
def initialize(attributes = {})
attributes.each do |attribute, value|
public_send("#{attribute}=", value)
end
end
end
class VirtusIntegers
include Virtus.model
ATTR_NAMES.each do |name|
attribute name, Integer
end
end
class AnimaIntegers
include Anima.new *ATTR_NAMES
end
class EthosIntegers
include Ethos::Entity
ATTR_NAMES.each do |name|
attribute name, Integer
end
end
Benchmark.ips do |x|
x.config(time: 1, warmup: 1)
integers = {attr0: 0, attr1: 1, attr2: 2, attr3: 3, attr4: 4, attr5: 5, attr6: 6, attr7: 7, attr8: 8, attr9: 9}
strings = {attr0: '0', attr1: '1', attr2: '2', attr3: '3', attr4: '4', attr5: '5', attr6: '6', attr7: '7', attr8: '8', attr9: '9'}
x.report('FastAttributes: without values ') { e = FastIntegers.new; ATTR_NAMES.each do |a| e.send a end }
x.report('FastAttributes: integer values for integer attributes ') { e = FastIntegers.new(integers); ATTR_NAMES.each do |a| e.send a end }
x.report('FastAttributes: string values for integer attributes ') { e = FastIntegers.new(strings); ATTR_NAMES.each do |a| e.send a end }
x.report('Attrio: without values ') { e = AttrioIntegers.new; ATTR_NAMES.each do |a| e.send a end }
x.report('Attrio: integer values for integer attributes ') { e = AttrioIntegers.new(integers); ATTR_NAMES.each do |a| e.send a end }
x.report('Attrio: string values for integer attributes ') { e = AttrioIntegers.new(strings); ATTR_NAMES.each do |a| e.send a end }
x.report('Virtus: without values ') { e = VirtusIntegers.new; ATTR_NAMES.each do |a| e.send a end }
x.report('Virtus: integer values for integer attributes ') { e = VirtusIntegers.new(integers); ATTR_NAMES.each do |a| e.send a end }
x.report('Virtus: string values for integer attributes ') { e = VirtusIntegers.new(strings); ATTR_NAMES.each do |a| e.send a end }
x.report('Anima: without values ') { e = AnimaIntegers.new(integers); ATTR_NAMES.each do |a| e.send a end }
x.report('Anima: integer values for integer attributes ') { e = AnimaIntegers.new(integers); ATTR_NAMES.each do |a| e.send a end }
x.report('Anima: string values for integer attributes ') { e = AnimaIntegers.new(strings); ATTR_NAMES.each do |a| e.send a end }
x.report('Ethos: without values ') { e = EthosIntegers.new; ATTR_NAMES.each do |a| e.send a end }
x.report('Ethos: integer values for integer attributes ') { e = EthosIntegers.new(integers); ATTR_NAMES.each do |a| e.send a end }
x.report('Ethos: string values for integer attributes ') { e = EthosIntegers.new(strings); ATTR_NAMES.each do |a| e.send a end }
x.compare!
end
Comparison:
FastAttributes: without values : 598430.6 i/s
Ethos: without values : 135310.1 i/s - 4.42x slower
FastAttributes: integer values for integer attributes : 101799.3 i/s - 5.88x slower
Ethos: integer values for integer attributes : 97076.2 i/s - 6.16x slower
Ethos: string values for integer attributes : 96645.0 i/s - 6.19x slower
FastAttributes: string values for integer attributes : 91458.5 i/s - 6.54x slower
Anima: integer values for integer attributes : 79142.7 i/s - 7.56x slower
Anima: string values for integer attributes : 78666.3 i/s - 7.61x slower
Anima: without values : 78617.2 i/s - 7.61x slower
Virtus: integer values for integer attributes : 26432.7 i/s - 22.64x slower
Attrio: integer values for integer attributes : 14882.0 i/s - 40.21x slower
Virtus: without values : 13857.3 i/s - 43.19x slower
Attrio: string values for integer attributes : 13714.0 i/s - 43.64x slower
Attrio: without values : 10029.0 i/s - 59.67x slower
Virtus: string values for integer attributes : 4215.9 i/s - 141.94x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment