Last active
November 28, 2022 01:02
-
-
Save goldeneggg/9ee48fa376fa1f81cf324f64ca4ca97e to your computer and use it in GitHub Desktop.
Benchmark Comparison - Hash vs Struct vs OpenStruct in Ruby 2.7.6
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
require 'benchmark/ips' | |
require 'benchmark/memory' | |
module Benchmarker | |
class Target | |
attr_reader :title, :code_block | |
def initialize(title:, code_block:) | |
@title = title | |
@code_block = code_block | |
end | |
end | |
class IpsConf | |
DEFAULT_TIME = 5 | |
DEFAULT_WARMUP = 2 | |
attr_reader :time, :warmup, :suite, :result_holder, :result_saver | |
def initialize(time: DEFAULT_TIME, | |
warmup: DEFAULT_WARMUP, | |
suite: nil, | |
result_holder: nil, | |
result_saver: nil) | |
@time = time | |
@warmup = warmup | |
# See: https://github.com/evanphx/benchmark-ips#custom-suite | |
@suite = suite | |
# See: https://github.com/evanphx/benchmark-ips#independent-benchmarking | |
@result_holder = result_holder | |
@result_saver = result_saver | |
end | |
end | |
class MemoryConf | |
attr_reader :result_holder | |
def initialize(result_holder: nil) | |
# See: https://github.com/michaelherold/benchmark-memory#hold-results-between-invocations | |
@result_holder = result_holder | |
end | |
end | |
class << self | |
def compare_ips(*targets, ipsconf: nil) | |
ic = | |
if ipsconf.nil? | |
IpsConf.new | |
else | |
ipsconf | |
end | |
Benchmark.ips do |x| | |
x.time = ic.time | |
x.warmup = ic.warmup | |
x.config(suite: ic.suite) unless ic.suite.nil? | |
targets.each do |target| | |
x.report(target.title) { target.code_block.call } | |
end | |
x.hold!(ic.result_holder) unless ic.result_holder.nil? | |
x.save!(ic.result_saver) unless ic.result_saver.nil? | |
x.compare! | |
end | |
end | |
def compare_memory(*targets, memoryconf: nil) | |
mc = | |
if memoryconf.nil? | |
MemoryConf.new | |
else | |
memoryconf | |
end | |
Benchmark.memory do |x| | |
targets.each do |target| | |
x.report(target.title) { target.code_block.call } | |
end | |
x.hold!(mc.result_holder) unless mc.result_holder.nil? | |
x.compare! | |
end | |
end | |
def compare(*targets, ipsconf: nil, memoryconf: nil) | |
compare_ips(*targets, ipsconf: ipsconf) | |
compare_memory(*targets, memoryconf: memoryconf) | |
end | |
end | |
end | |
User = Struct.new(:name, :age) | |
t1 = ::Benchmarker::Target.new( | |
title: 't1 is OpenStruct', | |
code_block: proc do | |
OpenStruct.new(:name => 'User', :age => 21) | |
end | |
) | |
t2 = ::Benchmarker::Target.new( | |
title: 't2 is Struct', | |
code_block: proc do | |
User.new('User', 21) | |
end | |
) | |
t3 = ::Benchmarker::Target.new( | |
title: 't3 is Hash', | |
code_block: proc do | |
{:name => 'User', :age => 21} | |
end | |
) | |
Benchmarker.compare(t1, t2, t3) |
Author
goldeneggg
commented
Nov 24, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment