Snippets

Yuji Hanamura Benchmark for Struct and OpenStruct of Ruby

Created by Yuji Hanamura
#!/usr/bin/env ruby
# frozen_string_literal: true

require "benchmark"
require "ostruct"

REP = 7_000_000

User = Struct.new(:name, :age)

USER = "User"
AGE = 21
HASH = { name: USER, age: AGE }.freeze

Benchmark.bmbm 20 do |x|
  x.report "OpenStruct hash style" do
    REP.times do |index|
      OpenStruct.new(HASH)
        .tap { |o| o[:name] }
        .tap { |o| o[:age] }
    end
  end

  x.report "OpenStruct object style" do
    REP.times do |index|
      OpenStruct.new(HASH)
        .tap { |o| o.name }
        .tap { |o| o.age }
    end
  end

  x.report "Struct hash style" do
    REP.times do |index|
      User.new(USER, AGE)
        .tap { |o| o[:name] }
        .tap { |o| o[:age] }
    end
  end

  x.report "Struct object style" do
    REP.times do |index|
      User.new(USER, AGE)
        .tap { |o| o.name }
        .tap { |o| o.age }
    end
  end

  x.report "Struct each hash style" do
    struct = Struct.new(:name, :age)
    REP.times do |index|
      struct.new(USER, AGE)
        .tap { |o| o[:name] }
        .tap { |o| o[:age] }
    end
  end

  x.report "Struct each object style" do
    struct = Struct.new(:name, :age)
    REP.times do |index|
      struct.new(USER, AGE)
        .tap { |o| o.name }
        .tap { |o| o.age }
    end
  end
end
$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]

$ ruby bench_struct_ostruct.rb
Rehearsal ------------------------------------------------------------
OpenStruct hash style      4.320000   0.030000   4.350000 (  3.811787)
OpenStruct object style   65.880000   0.500000  66.380000 ( 61.012074)
Struct hash style          1.970000   0.000000   1.970000 (  1.789689)
Struct object style        1.530000   0.010000   1.540000 (  1.452933)
Struct each hash style     2.260000   0.000000   2.260000 (  2.133236)
Struct each object style   1.640000   0.010000   1.650000 (  1.578425)
-------------------------------------------------- total: 78.150000sec

                               user     system      total        real
OpenStruct hash style      3.680000   0.020000   3.700000 (  3.671749)
OpenStruct object style   60.360000   0.510000  60.870000 ( 56.973101)
Struct hash style          2.010000   0.010000   2.020000 (  1.998026)
Struct object style        1.620000   0.010000   1.630000 (  1.615803)
Struct each hash style     1.920000   0.010000   1.930000 (  1.793344)
Struct each object style   1.620000   0.000000   1.620000 (  1.595888)
$ ruby -v
jruby 9.1.5.0 (2.3.1) 2016-09-07 036ce39 OpenJDK 64-Bit Server VM 25.111-b15 on 1.8.0_111-b15 +jit [linux-x86_64]

$ ruby bench_struct_ostruct.rb
Rehearsal ------------------------------------------------------------
OpenStruct hash style      5.130000   0.040000   5.170000 (  4.258238)
OpenStruct object style   70.240000   0.550000  70.790000 ( 63.509912)
Struct hash style          2.080000   0.010000   2.090000 (  1.861567)
Struct object style        1.750000   0.000000   1.750000 (  1.649185)
Struct each hash style     1.920000   0.010000   1.930000 (  1.829623)
Struct each object style   1.810000   0.010000   1.820000 (  1.733285)
-------------------------------------------------- total: 83.550000sec

                               user     system      total        real
OpenStruct hash style      4.900000   0.010000   4.910000 (  4.870477)
OpenStruct object style   68.000000   0.490000  68.490000 ( 64.333432)
Struct hash style          1.810000   0.010000   1.820000 (  1.811357)
Struct object style        1.530000   0.000000   1.530000 (  1.522939)
Struct each hash style     1.980000   0.010000   1.990000 (  1.886835)
Struct each object style   1.700000   0.010000   1.710000 (  1.690192)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.