Skip to content

Instantly share code, notes, and snippets.

@tristang
Created September 2, 2016 13:50
Show Gist options
  • Save tristang/a961fdc327e7d79439729a6c381227dc to your computer and use it in GitHub Desktop.
Save tristang/a961fdc327e7d79439729a6c381227dc to your computer and use it in GitHub Desktop.
OpenStruct vs Struct vs Hash performance
require 'benchmark'
require 'ostruct'
REP = 1000000
User = Struct.new(: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|
OpenStruct.new(:name => "User", :age => 21)
end
end
x.report 'OpenStruct fast' do
REP.times do |index|
OpenStruct.new(HASH)
end
end
x.report 'Struct slow' do
REP.times do |index|
User.new("User", 21)
end
end
x.report 'Struct fast' do
REP.times do |index|
User.new(USER, AGE)
end
end
x.report 'Hash slow' do
REP.times do |index|
{:name => 'User', :age => 21}
end
end
x.report 'Hash fast' do
REP.times do |index|
{:name => USER, :age => AGE}
end
end
end
user system total real
OpenStruct slow 8.150000 0.000000 8.150000 ( 8.153797)
OpenStruct fast 7.980000 0.000000 7.980000 ( 7.983929)
Struct slow 0.270000 0.000000 0.270000 ( 0.268514)
Struct fast 0.200000 0.000000 0.200000 ( 0.196950)
Hash slow 0.580000 0.000000 0.580000 ( 0.584160)
Hash fast 0.460000 0.000000 0.460000 ( 0.458502)
@azrazalea-debtbook
Copy link

In case anyone is interested, I added Data to the benchmarks and also included having to access each object 10 times. This shows Struct and Data outperforming Hash if you include that they access the data faster.

https://gist.github.com/azrazalea-debtbook/ae2d46eb43e5fec421da739461484577

                           user     system      total        real
OpenStruct slow        7.414383   0.022696   7.437079 (  7.437549)
OpenStruct fast        7.375381   0.024232   7.399613 (  7.433433)
Struct slow            0.723688   0.002673   0.726361 (  0.726363)
Struct fast            0.698972   0.002602   0.701574 (  0.701566)
Hash slow              1.162884   0.004929   1.167813 (  1.204769)
Hash fast              1.114190   0.003281   1.117471 (  1.119175)
Data slow              0.825655   0.002959   0.828614 (  0.828610)
Data fast              0.803010   0.002993   0.806003 (  0.806002)

@Rhoxio
Copy link

Rhoxio commented Aug 7, 2024

Ruby 3.3.4 on arm64 Macbook M3:

                           user     system      total        real
OpenStruct slow        3.804766   0.029756   3.834522 (  3.841408)
OpenStruct fast        3.601886   0.008229   3.610115 (  3.615765)
Struct slow            0.093334   0.000282   0.093616 (  0.093774)
Struct fast            0.081255   0.000151   0.081406 (  0.081472)
Hash slow              0.058589   0.000235   0.058824 (  0.058935)
Hash fast              0.047965   0.000229   0.048194 (  0.048267)

@azrazalea-debtbook
Copy link

Ruby 3.3.3 on arm64 Macbook M3 pro (on battery):

                           user     system      total        real
OpenStruct slow        4.448626   0.066791   4.515417 (  4.547827)
OpenStruct fast        4.273663   0.033246   4.306909 (  4.323059)
Struct slow            0.236373   0.001003   0.237376 (  0.237390)
Struct fast            0.228933   0.000859   0.229792 (  0.229795)
Hash slow              0.295569   0.001394   0.296963 (  0.296973)
Hash fast              0.265346   0.001351   0.266697 (  0.266701)
Data slow              0.350167   0.001732   0.351899 (  0.351904)
Data fast              0.342578   0.001556   0.344134 (  0.344140)

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