Created
September 2, 2016 13:50
-
-
Save tristang/a961fdc327e7d79439729a6c381227dc to your computer and use it in GitHub Desktop.
OpenStruct vs Struct vs Hash 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 | |
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 |
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
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) |
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)
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
In case anyone is interested, I added
Data
to the benchmarks and also included having to access each object 10 times. This showsStruct
andData
outperformingHash
if you include that they access the data faster.https://gist.github.com/azrazalea-debtbook/ae2d46eb43e5fec421da739461484577