Skip to content

Instantly share code, notes, and snippets.

@elct9620
Created April 14, 2020 06:07
Show Gist options
  • 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.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