Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save azrazalea-debtbook/ae2d46eb43e5fec421da739461484577 to your computer and use it in GitHub Desktop.
Save azrazalea-debtbook/ae2d46eb43e5fec421da739461484577 to your computer and use it in GitHub Desktop.
OpenStruct vs Struct vs Hash vs Data performance
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
# 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)
@azrazalea-debtbook
Copy link
Author

# NOTE: Ran on arm64 Macbook m3 pro with ruby 3.3.3 compiled by ASDF/ruby-build
                           user     system      total        real
OpenStruct slow        4.382297   0.025886   4.408183 (  4.408214)
OpenStruct fast        4.312767   0.019371   4.332138 (  4.332138)
Struct slow            0.323689   0.001325   0.325014 (  0.325016)
Struct fast            0.316886   0.001088   0.317974 (  0.317973)
Hash slow              0.297655   0.001035   0.298690 (  0.298690)
Hash fast              0.284033   0.000986   0.285019 (  0.285019)
Data slow              0.413199   0.001474   0.414673 (  0.414676)
Data fast              0.402979   0.001253   0.404232 (  0.404234)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment