Last active
June 17, 2022 01:09
-
-
Save texpert/f5ffa6352441bfa7871e1747a8912782 to your computer and use it in GitHub Desktop.
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
13:30 $ rvm ls | |
ruby-2.4.9 [ x86_64 ] | |
=> ruby-2.5.1 [ x86_64 ] | |
* ruby-2.5.5 [ x86_64 ] | |
ruby-2.5.7 [ x86_64 ] | |
ruby-2.6.5 [ x86_64 ] | |
13:28 $ ruby poro_vs_attr_extras.rb | |
Warming up -------------------------------------- | |
PORO Parallel Assignment | |
77.276k i/100ms | |
PORO Sequential Assignment | |
94.895k i/100ms | |
AttrExtras Private 7.943k i/100ms | |
Calculating ------------------------------------- | |
PORO Parallel Assignment | |
1.195M (± 2.2%) i/s - 11.746M in 10.051377s | |
PORO Sequential Assignment | |
1.728M (± 0.8%) i/s - 17.271M in 10.029018s | |
AttrExtras Private 91.919k (± 0.4%) i/s - 921.388k in 10.029671s | |
with 95.0% confidence | |
Comparison: | |
PORO Sequential Assignment: 1727784.5 i/s | |
PORO Parallel Assignment: 1195351.2 i/s - 1.45x (± 0.03) slower | |
AttrExtras Private: 91919.1 i/s - 18.80x (± 0.17) slower | |
with 95.0% confidence | |
Code: | |
----------------------------- | |
# frozen_string_literal: true | |
# Install the necessary gems: | |
# gem install benchmark-ips | |
# gem install kalibera | |
require 'benchmark/ips' | |
require 'attr_extras' | |
# Enable and start GC before each job run. Disable GC afterwards. | |
# | |
# Inspired by https://www.omniref.com/ruby/2.2.1/symbols/Benchmark/bm?#annotation=4095926&line=182 | |
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.disable | |
end | |
end | |
suite = GCSuite.new | |
User = Struct.new(:id, :stuff) | |
user1 = User.new(1, stuff: rand(1000)) | |
user2 = User.new(2, stuff: rand(1000)) | |
class PoroParallelAssignment | |
def initialize(user1, user2) | |
@user1, @user2 = user1, user2 | |
end | |
private | |
attr_reader :user1, :user2 | |
end | |
class PoroSequentialAssignment | |
def initialize(user1, user2) | |
@user1 = user1 | |
@user2 = user2 | |
end | |
private | |
attr_reader :user1, :user2 | |
end | |
class AttrExtrasPrivate | |
pattr_initialize :user1, :user2 | |
end | |
Benchmark.ips do |x| | |
x.config(:time => 10, :warmup => 5, :stats => :bootstrap, :confidence => 95) | |
x.report('PORO Parallel Assignment') do | |
PoroParallelAssignment.new(user1, user2) | |
end | |
x.report('PORO Sequential Assignment') do | |
PoroSequentialAssignment.new(user1, user2) | |
end | |
x.report('AttrExtras Private') do | |
AttrExtrasPrivate.new(user1, user2) | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment