-
-
Save wasifhossain/cf61c5e0cf9ef51e63d1cdd83a8eb7ef 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
# frozen_string_literal: true | |
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" | |
gem "activerecord", path: "." | |
gem "activesupport", path: "../activesupport" | |
gem "activemodel", path: "../activemodel" | |
gem "benchmark-ips" | |
gem 'pg' | |
gem "pry" | |
end | |
require "active_record" | |
require "benchmark/ips" | |
conn = { adapter: "postgresql", database: "bench" } | |
ActiveRecord::Base.establish_connection(conn) | |
class User < ActiveRecord::Base | |
connection.create_table :users, force: true do |t| | |
t.timestamps | |
end | |
end | |
user = User.create! | |
user.reload | |
attributes = user.instance_variable_get(:@attributes).deep_dup | |
puts "New: #{user.updated_at_before_type_cast.to_param}" | |
puts "New keeping format: #{user.updated_at_before_type_cast.delete('- :.').to_param}" | |
puts "Original: #{user.updated_at.utc.to_s(:usec).to_param}" | |
Benchmark.ips do |x| | |
x.report("New") do | |
user.instance_variable_set(:@attributes, attributes.deep_dup) | |
user.updated_at_before_type_cast.to_param | |
end | |
x.report "New keeping format" do | |
user.instance_variable_set(:@attributes, attributes.deep_dup) | |
user.updated_at_before_type_cast.delete('- :.') | |
end | |
x.report "Original" do | |
user.instance_variable_set(:@attributes, attributes.deep_dup) | |
user.updated_at.utc.to_s(:usec).to_param | |
end | |
x.compare! | |
end | |
# New: 2018-10-11 01:28:30.836427 | |
# New keeping format: 20181011012830836427 | |
# Original: 20181011012830836427 | |
# Warming up -------------------------------------- | |
# New 26.080k i/100ms | |
# New keeping format 23.679k i/100ms | |
# Original 7.127k i/100ms | |
# Calculating ------------------------------------- | |
# New 292.214k (± 9.0%) i/s - 1.460M in 5.033789s | |
# New keeping format 250.280k (± 8.5%) i/s - 1.255M in 5.045426s | |
# Original 69.712k (± 5.5%) i/s - 349.223k in 5.024369s | |
# | |
# Comparison: | |
# New: 292214.1 i/s | |
# New keeping format: 250279.6 i/s - same-ish: difference falls within error | |
# Original: 69711.7 i/s - 4.19x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment