Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created November 15, 2017 22:18
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 kamipo/8ed73d760112cfa5f6263c9413633419 to your computer and use it in GitHub Desktop.
Save kamipo/8ed73d760112cfa5f6263c9413633419 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
gem "bundler", "< 1.16"
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
gem "sqlite3"
gem "benchmark-ips"
end
require "active_record"
require "benchmark/ips"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
create_table :foos do |t|
t.integer :value, default: 0
end
end
class Foo < ActiveRecord::Base
end
class SubFoo < Foo
end
foo = SubFoo.create!
Benchmark.ips do |x|
x.report("_update_record") { foo.value += 1; foo.save! }
end
@kamipo
Copy link
Author

kamipo commented Nov 15, 2017

Before:

Warming up --------------------------------------
      _update_record   150.000  i/100ms
Calculating -------------------------------------
      _update_record      1.548k (±12.3%) i/s -      7.650k in   5.042603s

After:

Warming up --------------------------------------
      _update_record   201.000  i/100ms
Calculating -------------------------------------
      _update_record      2.002k (±12.8%) i/s -      9.849k in   5.027681s

30% faster for STI classes.

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