Skip to content

Instantly share code, notes, and snippets.

View texpert's full-sized avatar

Aurel Branzeanu texpert

View GitHub Profile
@texpert
texpert / const_lookup.rb
Last active June 24, 2022 16:57
Ruby constant lookup vs const_get benchmark
Warming up --------------------------------------
lookup 1.241M i/100ms
const_get 1.111M i/100ms
Calculating -------------------------------------
lookup 13.510M (± 0.7%) i/s - 135.305M in 10.031861s
const_get 11.373M (± 0.5%) i/s - 114.446M in 10.072033s
with 95.0% confidence
Comparison:
lookup: 13509513.8 i/s
@texpert
texpert / DCI_and_MRI_methods_caching
Created March 19, 2021 10:39
DCI pattern in Ruby and methods cache in MRI
An interesting line-up of articles about DCI pattern in Ruby and methods cache in MRI
(one of the reasons to avoid OpenStruct is methods cache invalidation on every OpenStruct instantiation):
https://tonyarcieri.com/dci-in-ruby-is-completely-broken
https://dec0de.me/2013/02/dci-performance
https://jamesgolick.com/2013/4/14/mris-method-caches.html
https://shopify.engineering/17489064-tuning-rubys-global-method-cache
https://mensfeld.pl/2015/04/ruby-global-method-cache-invalidation-impact-on-a-single-and-multithreaded-applications/
https://tenderlovemaking.com/2015/12/23/inline-caching-in-mri.html
/bin/bash -c "/Users/thunder/.rvm/bin/rvm ruby-2.3.7@mejuri do /Users/thunder/.rvm/rubies/ruby-2.3.7/bin/ruby '/Users/thunder/Library/Application Support/JetBrains/RubyMine2020.2/scratches/scratch.rb'"
Warming up --------------------------------------
Direct interpolation 426.585k i/100ms
Percent interpolation
232.103k i/100ms
Plus concatenation 507.047k i/100ms
Calculating -------------------------------------
Direct interpolation 4.393M (± 0.3%) i/s - 43.938M in 10.005452s
Percent interpolation
brew install rbenv/tap/openssl@1.0
rvm reinstall ruby-2.2.0 --with-openssl-dir='/usr/local/opt/openssl@1.0'
/bin/bash -c "/home/thunder/.rvm/bin/rvm ruby-2.5.1 do /home/thunder/.rvm/rubies/ruby-2.5.1/bin/ruby /home/thunder/.RubyMine2019.3/config/scratches/scratch_1.rb"
Warming up --------------------------------------
Active Support symbolize_keys
37.892k i/100ms
Hash keys map to_sym 60.148k i/100ms
Calculating -------------------------------------
Active Support symbolize_keys
562.840k (± 1.4%) i/s - 5.608M in 10.047830s
Hash keys map to_sym 767.850k (± 1.1%) i/s - 7.699M in 10.048546s
with 95.0% confidence
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 --------------------------------------
# frozen_string_literal: true
# Install the necessary gems:
# gem install benchmark-ips
# gem install kalibera
require 'benchmark/ips'
ARRAY = (1..100).to_a.map { |e| [e, e] }
@texpert
texpert / array2hash_each_inject_etc.rb
Last active June 17, 2022 01:11
Each, each_with_object, inject, merge Array to Hash transforming comparison
Warming up --------------------------------------
assign&return 21.225k i/100ms
each 23.473k i/100ms
merge 3.283k i/100ms
merge! 9.870k i/100ms
map with tuples 19.764k i/100ms
each_with_object 21.362k i/100ms
Calculating -------------------------------------
assign&return 229.939k (± 5.2%) i/s - 2.314M in 10.087643s
each 256.244k (± 7.3%) i/s - 2.559M in 10.028367s
Legend:
=> in the meaning column means there's a non-boolean return value
empty columns in Prior Name mean the method didn't exist before
* before a new method name means it's an alias to the prior method
Prior Name New Name Meaning
attribute_was(attr_name) *attribute_in_database(attr_name) current database value of attribute
attribute_change(attr_name) *attribute_change_to_be_saved => the changed value for attribute
attribute_changed?(attr_name) *will_save_change_to_attribute?(attr_name) was attribute modified since last save
Warming up --------------------------------------
Array5#include? 196.263k i/100ms
Array5#find 102.092k i/100ms
Array5#bsearch 173.777k i/100ms
Array5#detect 97.253k i/100ms
Calculating -------------------------------------
Array5#include? 5.089M (±11.0%) i/s - 25.122M in 5.018964s
Array5#find 1.455M (± 4.9%) i/s - 7.351M in 5.062443s
Array5#bsearch 3.555M (± 6.6%) i/s - 17.725M in 5.007809s
Array5#detect 1.496M (± 5.9%) i/s - 7.488M in 5.021683s