Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'objspace'
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection adapter: "sqlite3",
database: ":memory:"
ActiveRecord::Base.connection.instance_eval do
create_table(:articles) { |t| t.string :name }
end
class Article < ActiveRecord::Base; end
a = Article.create name: "foo"
a = Article.find a.id
Benchmark.ips do |b|
b.report("find") { Article.find a.id }
b.report("initialize") { Article.new }
b.report("create") { Article.create name: "bar" }
end
pete@balloon:~/projects/rails/activerecord$ for i in 1 2 3 4 5 ; do echo $i ; git checkout master ; bundle exec ruby benchmark_callback_send.rb ; git checkout remove-dynamic-send-on-built-in-callbacks ; bundle exec ruby benchmark_callback_send.rb ; done
1
M Gemfile
Switched to branch 'master'
Calculating -------------------------------------
find 1204 i/100ms
initialize 5076 i/100ms
create 215 i/100ms
-------------------------------------------------
find 12956.7 (±5.8%) i/s - 65016 in 5.040725s
initialize 72725.2 (±3.4%) i/s - 365472 in 5.031126s
create 2271.3 (±2.3%) i/s - 11395 in 5.019555s
M Gemfile
Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
Calculating -------------------------------------
find 1163 i/100ms
initialize 4560 i/100ms
create 210 i/100ms
-------------------------------------------------
find 13378.3 (±2.7%) i/s - 67454 in 5.045922s
initialize 73790.2 (±1.0%) i/s - 369360 in 5.006085s
create 2289.2 (±1.8%) i/s - 11550 in 5.047354s
2
M Gemfile
Switched to branch 'master'
Calculating -------------------------------------
find 903 i/100ms
initialize 4542 i/100ms
create 223 i/100ms
-------------------------------------------------
find 12198.5 (±10.6%) i/s - 60501 in 5.023331s
initialize 74574.6 (±4.7%) i/s - 372444 in 5.006968s
create 2111.7 (±11.0%) i/s - 10481 in 5.030374s
M Gemfile
Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
Calculating -------------------------------------
find 1205 i/100ms
initialize 5066 i/100ms
create 222 i/100ms
-------------------------------------------------
find 13481.8 (±1.2%) i/s - 67480 in 5.005967s
initialize 76122.7 (±1.8%) i/s - 385016 in 5.059663s
create 2306.3 (±1.4%) i/s - 11544 in 5.006464s
3
M Gemfile
Switched to branch 'master'
Calculating -------------------------------------
find 1184 i/100ms
initialize 4979 i/100ms
create 230 i/100ms
-------------------------------------------------
find 13438.9 (±0.7%) i/s - 67488 in 5.022101s
initialize 76661.2 (±1.7%) i/s - 383383 in 5.002511s
create 2345.5 (±1.7%) i/s - 11960 in 5.100498s
M Gemfile
Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
Calculating -------------------------------------
find 1228 i/100ms
initialize 4876 i/100ms
create 230 i/100ms
-------------------------------------------------
find 13960.3 (±0.9%) i/s - 69996 in 5.014323s
initialize 76569.4 (±1.7%) i/s - 385204 in 5.032409s
create 2346.7 (±1.7%) i/s - 11960 in 5.098060s
4
M Gemfile
Switched to branch 'master'
Calculating -------------------------------------
find 1202 i/100ms
initialize 5173 i/100ms
create 229 i/100ms
-------------------------------------------------
find 13525.2 (±0.9%) i/s - 68514 in 5.066126s
initialize 77562.8 (±1.9%) i/s - 387975 in 5.003947s
create 2349.4 (±1.8%) i/s - 11908 in 5.070312s
M Gemfile
Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
Calculating -------------------------------------
find 1214 i/100ms
initialize 4964 i/100ms
create 231 i/100ms
-------------------------------------------------
find 13725.5 (±0.8%) i/s - 69198 in 5.041848s
initialize 74345.0 (±2.6%) i/s - 372300 in 5.011374s
create 2353.5 (±2.3%) i/s - 11781 in 5.008741s
5
M Gemfile
Switched to branch 'master'
Calculating -------------------------------------
find 1203 i/100ms
initialize 5005 i/100ms
create 225 i/100ms
-------------------------------------------------
find 13462.7 (±2.0%) i/s - 67368 in 5.006160s
initialize 75286.3 (±2.2%) i/s - 380380 in 5.055039s
create 2309.1 (±2.3%) i/s - 11700 in 5.069729s
M Gemfile
Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
Calculating -------------------------------------
find 1228 i/100ms
initialize 5048 i/100ms
create 230 i/100ms
-------------------------------------------------
find 13713.6 (±2.3%) i/s - 68768 in 5.017460s
initialize 76114.5 (±1.5%) i/s - 383648 in 5.041556s
create 2373.8 (±1.4%) i/s - 11960 in 5.039322s
@phiggins

This comment has been minimized.

Copy link
Owner Author

phiggins commented Sep 28, 2014

operation master remove-dynamic-send-on-built-in-callbacks percent improvement
find 12956.7 + 12198.5 + 13438.9 + 13525.2 + 13462.7 = 65582.0 / 5 = 13116.4 13378.3 + 13481.8 + 13960.3 + 13725.5 + 13713.6 = 68259.5 / 5 = 13651.9 4.08%
initialize 72725.2 + 74574.6 + 76661.2 + 77562.8 + 75286.3 = 376810.1 / 5 = 75362.0 73790.2 + 76122.7 + 76569.4 + 74345.0 + 76114.5 = 376941.8 / 5 = 75388.4 0.03%
create 2271.3 + 2111.7 + 2345.5 + 2349.4 + 2309.1 = 11387.0 / 5 = 2277.4 2289.2 + 2306.3 + 2346.7 + 2353.5 + 2373.8 = 11669.5 / 5 = 2333.9 2.48%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.