Last active
March 15, 2019 11:44
-
-
Save simonc/6dd7a11ea050045c424cf0ac8bd04cac to your computer and use it in GitHub Desktop.
Benchmark between data structures
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
# frozen_string_literal: true | |
require 'bundler/inline' | |
require 'ostruct' | |
gemfile do | |
source "https://rubygems.org" | |
gem 'benchmark-ips', require: 'benchmark/ips' | |
gem 'benchmark-memory', require: 'benchmark/memory' | |
end | |
ValueStruct = Struct.new(:key, :other) | |
class ValueObject | |
attr_reader :key, :other | |
def initialize(key, other) | |
@key = key | |
@other = other | |
end | |
end | |
class ValueObject2 | |
attr_reader :key, :other | |
def initialize(key:, other:) | |
@key = key | |
@other = other | |
end | |
end | |
Benchmark.ips do |x| | |
x.config(time: 5, warmup: 2) | |
x.report("hash") { h = { key: "value", other: "other value" }; h[:other] } | |
x.report("ostruct") { o = OpenStruct.new(key: "value", other: "other value"); o.other } | |
x.report("struct") { s = ValueStruct.new(key: "value", other: "other value"); s.other } | |
x.report("class") { s = ValueObject.new("value", "other value"); s.other } | |
x.report("kargs-class") { s = ValueObject2.new(key: "value", other: "other value"); s.other } | |
x.compare! | |
end | |
Benchmark.memory do |x| | |
x.report("hash") { h = { key: "value", other: "other value" }; h[:other] } | |
x.report("ostruct") { o = OpenStruct.new(key: "value", other: "other value"); o.other } | |
x.report("struct") { s = ValueStruct.new(key: "value", other: "other value"); s.other } | |
x.report("class") { s = ValueObject.new("value", "other value"); s.other } | |
x.report("kargs-class") { s = ValueObject2.new(key: "value", other: "other value"); s.other } | |
x.compare! | |
end |
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
#################################################################################################### | |
Iteration per second | |
#################################################################################################### | |
Warming up -------------------------------------- | |
hash 110.273k i/100ms | |
ostruct 13.676k i/100ms | |
struct 92.467k i/100ms | |
class 188.862k i/100ms | |
kargs-class 56.304k i/100ms | |
Calculating ------------------------------------- | |
hash 1.626M (± 5.0%) i/s - 8.160M in 5.032514s | |
ostruct 145.801k (± 3.5%) i/s - 738.504k in 5.072197s | |
struct 1.243M (± 7.0%) i/s - 6.195M in 5.011539s | |
class 3.742M (± 7.4%) i/s - 18.697M in 5.033734s | |
kargs-class 693.452k (± 6.1%) i/s - 3.491M in 5.058219s | |
Comparison: | |
class: 3741830.7 i/s | |
hash: 1625872.7 i/s - 2.30x slower | |
struct: 1243438.7 i/s - 3.01x slower | |
kargs-class: 693451.7 i/s - 5.40x slower | |
ostruct: 145801.1 i/s - 25.66x slower | |
#################################################################################################### | |
Memory usage (Object allocations) | |
#################################################################################################### | |
Calculating ------------------------------------- | |
hash 192.000 memsize ( 0.000 retained) | |
1.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
ostruct 1.384k memsize ( 0.000 retained) | |
11.000 objects ( 0.000 retained) | |
2.000 strings ( 0.000 retained) | |
struct 232.000 memsize ( 0.000 retained) | |
2.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
class 40.000 memsize ( 0.000 retained) | |
1.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
kargs-class 424.000 memsize ( 0.000 retained) | |
3.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
Comparison: | |
class: 40 allocated | |
hash: 192 allocated - 4.80x more | |
struct: 232 allocated - 5.80x more | |
kargs-class: 424 allocated - 10.60x more | |
ostruct: 1384 allocated - 34.60x more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment