-
-
Save azrazalea-debtbook/ae2d46eb43e5fec421da739461484577 to your computer and use it in GitHub Desktop.
OpenStruct vs Struct vs Hash vs Data performance
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' | |
require 'ostruct' | |
REP = 1000000 | |
ACCESS_REP = 4 | |
User = Struct.new(:name, :age) | |
UserData = Data.define(:name, :age) | |
USER = "User".freeze | |
AGE = 21 | |
HASH = {:name => USER, :age => AGE}.freeze | |
Benchmark.bm 20 do |x| | |
x.report 'OpenStruct slow' do | |
REP.times do |index| | |
instance = OpenStruct.new(:name => "User", :age => 21) | |
ACCESS_REP.times do | |
[instance.name, instance.age] | |
end | |
end | |
end | |
x.report 'OpenStruct fast' do | |
REP.times do |index| | |
instance = OpenStruct.new(HASH) | |
ACCESS_REP.times do | |
[instance.name, instance.age] | |
end | |
end | |
end | |
x.report 'Struct slow' do | |
REP.times do |index| | |
instance = User.new("User", 21) | |
ACCESS_REP.times do | |
[instance.name, instance.age] | |
end | |
end | |
end | |
x.report 'Struct fast' do | |
REP.times do |index| | |
instance = User.new(USER, AGE) | |
ACCESS_REP.times do | |
[instance.name, instance.age] | |
end | |
end | |
end | |
x.report 'Hash slow' do | |
REP.times do |index| | |
instance = {:name => 'User', :age => 21} | |
ACCESS_REP.times do | |
[instance[:name], instance[:age]] | |
end | |
end | |
end | |
x.report 'Hash fast' do | |
REP.times do |index| | |
instance = {:name => USER, :age => AGE} | |
ACCESS_REP.times do | |
[instance[:name], instance[:age]] | |
end | |
end | |
end | |
x.report 'Data slow' do | |
REP.times do |index| | |
instance = UserData.new("User", 21) | |
ACCESS_REP.times do | |
[instance.name, instance.age] | |
end | |
end | |
end | |
x.report 'Data fast' do | |
REP.times do |index| | |
instance = UserData.new(USER, AGE) | |
ACCESS_REP.times do | |
[instance.name, instance.age] | |
end | |
end | |
end | |
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
# NOTE: Ran on arm64 Macbook m1 with ruby 3.2.2 compiled by ASDF/ruby-build | |
user system total real | |
OpenStruct slow 6.366970 0.025408 6.392378 ( 6.392326) | |
OpenStruct fast 6.321198 0.020139 6.341337 ( 6.341283) | |
Struct slow 0.387496 0.001697 0.389193 ( 0.389192) | |
Struct fast 0.364341 0.001684 0.366025 ( 0.366025) | |
Hash slow 0.537750 0.001559 0.539309 ( 0.539306) | |
Hash fast 0.520458 0.001227 0.521685 ( 0.521703) | |
Data slow 0.493237 0.001876 0.495113 ( 0.495149) | |
Data fast 0.469824 0.001703 0.471527 ( 0.471534) |
Author
azrazalea-debtbook
commented
Aug 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment