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)
@skrix
Copy link

skrix commented Jan 10, 2020

ruby 2.6.3 looks like hashes is faster than structs...

                           user     system      total        real
OpenStruct slow        0.760257   0.002922   0.763179 (  0.766513)
OpenStruct fast        0.597584   0.002368   0.599952 (  0.602767)
Struct slow            0.257806   0.003551   0.261357 (  0.265820)
Struct fast            0.220372   0.000379   0.220751 (  0.221160)
Hash slow              0.162665   0.001523   0.164188 (  0.166065)
Hash fast              0.128237   0.000906   0.129143 (  0.130054)

@gffcoutinho
Copy link

Ruby 3.0.2

                           user     system      total        real
OpenStruct slow        8.047000   0.000000   8.047000 (  8.061700)
OpenStruct fast        8.328000   0.016000   8.344000 (  8.341365)
Struct slow            0.157000   0.000000   0.157000 (  0.150189)
Struct fast            0.125000   0.000000   0.125000 (  0.125478)
Hash slow              0.093000   0.000000   0.093000 (  0.092687)
Hash fast              0.078000   0.000000   0.078000 (  0.077509)

@gildemberg-santos
Copy link

Ruby 2.5.1

                           user     system      total        real
OpenStruct slow        0.796528   0.000174   0.796702 (  0.800543)
OpenStruct fast        0.579909   0.000018   0.579927 (  0.582189)
Struct slow            0.152336   0.000000   0.152336 (  0.152991)
Struct fast            0.132024   0.000000   0.132024 (  0.132460)
Hash slow              0.217203   0.000000   0.217203 (  0.217984)
Hash fast              0.205297   0.000000   0.205297 (  0.205531)

@azrazalea-debtbook
Copy link

Ruby 3.2.2 arm64 macbook m1:

                           user     system      total        real
OpenStruct slow        5.198411   0.017713   5.216124 (  5.216191)
OpenStruct fast        5.079115   0.015835   5.094950 (  5.094971)
Struct slow            0.123186   0.000471   0.123657 (  0.123657)
Struct fast            0.103163   0.000209   0.103372 (  0.103370)
Hash slow              0.079912   0.000418   0.080330 (  0.080330)
Hash fast              0.070340   0.000254   0.070594 (  0.070595)

@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)

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