Skip to content

Instantly share code, notes, and snippets.

@elct9620
Created April 14, 2020 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elct9620/afd3743da3a61dde699825dd092a4144 to your computer and use it in GitHub Desktop.
Save elct9620/afd3743da3a61dde699825dd092a4144 to your computer and use it in GitHub Desktop.
Ruby's Struct and Class benchmark (bemchmark-ips)
# frozen_string_literal: true
require 'bundler/inline'
require 'ostruct'
gemfile do
gem 'benchmark-ips'
end
GeoStruct = Struct.new(:latitude, :longitude) do
def pair
[latitude, longitude]
end
end
# :nodoc:
class GeoClass
attr_reader :latitude, :longitude
def initialize(latitude, longitude)
@latitude = latitude
@longitude = longitude
end
def to_a
[@latitude, @longitude]
end
end
# :nodoc:
class GCSuite
def warming(*)
run_gc
end
def running(*)
run_gc
end
def warmup_stats(*); end
def add_report(*); end
private
def run_gc
GC.enable
GC.start
GC.start
GC.start
GC.disable
end
end
suite = GCSuite.new
def challenge_new(bm)
bm.report('GeoStruct.new') { GeoStruct.new(11.1, 12.1) }
bm.report('GeoClass.new') { GeoClass.new(11.1, 12.1) }
end
def challenge_access(bm)
geo_s = GeoStruct.new(11.1, 12.1)
geo_c = GeoClass.new(11.1, 12.1)
bm.report('GeoStruct#to_a') do
geo_s.to_a
end
bm.report('GeoStruct#pair') do
geo_s.to_a
end
bm.report('GeoClass#to_a') do
geo_c.to_a
end
end
def challenge_attr(bm)
geo_s = GeoStruct.new(11.1, 12.1)
geo_c = GeoClass.new(11.1, 12.1)
bm.report('GeoStruct#latitude') { geo_s.latitude }
bm.report('GeoClass#to_a') { geo_c.latitude }
end
puts '=== New without GC ==='
Benchmark.ips do |bm|
bm.config(suite: suite)
challenge_new(bm)
end
puts '=== New with GC ==='
Benchmark.ips do |bm|
challenge_new(bm)
end
puts '=== To Array without GC ==='
Benchmark.ips do |bm|
bm.config(suite: suite)
challenge_access(bm)
end
puts '=== To Array with GC ==='
Benchmark.ips do |bm|
challenge_access(bm)
end
puts '=== Get attribute without GC ==='
Benchmark.ips do |bm|
bm.config(suite: suite)
challenge_attr(bm)
end
puts '=== Get attribute Array with GC ==='
Benchmark.ips do |bm|
challenge_attr(bm)
end
@elct9620
Copy link
Author

Ruby 2.6.3

=== New without GC ===
Warming up --------------------------------------
       GeoStruct.new   261.178k i/100ms
        GeoClass.new   217.328k i/100ms
Calculating -------------------------------------
       GeoStruct.new      5.217M (±17.9%) i/s -     24.812M in   5.038213s
        GeoClass.new      3.990M (±47.9%) i/s -     16.517M in   5.068718s
=== New with GC ===
Warming up --------------------------------------
       GeoStruct.new   286.308k i/100ms
        GeoClass.new   281.180k i/100ms
Calculating -------------------------------------
       GeoStruct.new      4.045M (±42.7%) i/s -     17.465M in   5.023700s
        GeoClass.new      5.159M (±32.4%) i/s -     22.776M in   5.045996s
=== To Array without GC ===
Warming up --------------------------------------
      GeoStruct#to_a   333.200k i/100ms
      GeoStruct#pair   252.160k i/100ms
       GeoClass#to_a   243.564k i/100ms
Calculating -------------------------------------
      GeoStruct#to_a      9.180M (±24.4%) i/s -     42.316M in   5.028919s
      GeoStruct#pair      7.619M (±71.6%) i/s -     23.955M in   5.031207s
       GeoClass#to_a      6.395M (±85.0%) i/s -     18.511M in   5.013245s
=== To Array with GC ===
Warming up --------------------------------------
      GeoStruct#to_a   381.146k i/100ms
      GeoStruct#pair   381.196k i/100ms
       GeoClass#to_a   363.260k i/100ms
Calculating -------------------------------------
      GeoStruct#to_a      6.709M (±75.2%) i/s -     23.250M in   5.066735s
      GeoStruct#pair     10.034M (±50.8%) i/s -     35.070M in   5.023351s
       GeoClass#to_a     11.427M (±46.4%) i/s -     35.963M in   5.004686s
=== Get attribute without GC ===
Warming up --------------------------------------
  GeoStruct#latitude   403.042k i/100ms
       GeoClass#to_a   409.708k i/100ms
Calculating -------------------------------------
  GeoStruct#latitude     20.264M (± 3.3%) i/s -    101.567M in   5.017955s
       GeoClass#to_a     22.356M (± 3.7%) i/s -    111.850M in   5.010217s
=== Get attribute Array with GC ===
Warming up --------------------------------------
  GeoStruct#latitude   380.181k i/100ms
       GeoClass#to_a   392.360k i/100ms
Calculating -------------------------------------
  GeoStruct#latitude     18.987M (± 6.1%) i/s -     94.285M in   5.007170s
       GeoClass#to_a     22.004M (± 3.1%) i/s -    110.253M in   5.015872s

@elct9620
Copy link
Author

Ruby 2.7.0

=== New without GC ===
Warming up --------------------------------------
       GeoStruct.new   236.497k i/100ms
        GeoClass.new   198.000k i/100ms
Calculating -------------------------------------
       GeoStruct.new      4.389M (±14.4%) i/s -     21.285M in   5.024704s
        GeoClass.new      3.249M (±39.6%) i/s -     14.058M in   5.002599s
=== New with GC ===
Warming up --------------------------------------
       GeoStruct.new   252.087k i/100ms
        GeoClass.new   240.505k i/100ms
Calculating -------------------------------------
       GeoStruct.new      3.531M (±38.0%) i/s -     15.377M in   5.000629s
        GeoClass.new      4.154M (±28.0%) i/s -     19.000M in   5.046631s
=== To Array without GC ===
Warming up --------------------------------------
      GeoStruct#to_a   302.547k i/100ms
      GeoStruct#pair   243.558k i/100ms
       GeoClass#to_a   235.393k i/100ms
Calculating -------------------------------------
      GeoStruct#to_a      8.484M (±24.3%) i/s -     38.423M in   5.031653s
      GeoStruct#pair      6.878M (±67.5%) i/s -     23.138M in   5.031955s
       GeoClass#to_a      5.917M (±76.1%) i/s -     19.067M in   5.018018s
=== To Array with GC ===
Warming up --------------------------------------
      GeoStruct#to_a   357.822k i/100ms
      GeoStruct#pair   358.513k i/100ms
       GeoClass#to_a   346.302k i/100ms
Calculating -------------------------------------
      GeoStruct#to_a      6.008M (±72.8%) i/s -     21.111M in   5.092882s
      GeoStruct#pair      8.838M (±59.4%) i/s -     25.454M in   5.063972s
       GeoClass#to_a      9.306M (±55.2%) i/s -     27.358M in   5.044677s
=== Get attribute without GC ===
Warming up --------------------------------------
  GeoStruct#latitude   377.039k i/100ms
       GeoClass#to_a   393.776k i/100ms
Calculating -------------------------------------
  GeoStruct#latitude     16.557M (± 3.2%) i/s -     82.949M in   5.015209s
       GeoClass#to_a     19.554M (± 3.5%) i/s -     98.050M in   5.020803s
=== Get attribute Array with GC ===
Warming up --------------------------------------
  GeoStruct#latitude   375.472k i/100ms
       GeoClass#to_a   390.095k i/100ms
Calculating -------------------------------------
  GeoStruct#latitude     16.576M (± 3.1%) i/s -     82.979M in   5.010949s
       GeoClass#to_a     19.533M (± 3.5%) i/s -     97.914M in   5.019195s

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