Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
ActiveRecord group_by performance. All the "USING BLOCK" tests are directly or indirectly using `Enumerable#group_by`, whereas the "USING SYMBOL" tests are using the new `ActiveRecord::FinderMethods#group_by`.
require 'benchmark'
require File.expand_path('../../../load_paths', __FILE__)
require "active_record"
RECORDS = 20000
conn = { :adapter => 'sqlite3', :database => ':memory:' }
ActiveRecord::Base.establish_connection(conn)
class User < ActiveRecord::Base
connection.create_table :users, :force => true do |t|
t.string :name, :role
t.timestamps
end
end
puts 'Generating data...'
module ActiveRecord
class Faker
LOREM = %Q{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse non aliquet diam. Curabitur vel urna metus, quis malesuada elit.
Integer consequat tincidunt felis. Etiam non erat dolor. Vivamus imperdiet nibh sit amet diam eleifend id posuere diam malesuada. Mauris at accumsan sem.
Donec id lorem neque. Fusce erat lorem, ornare eu congue vitae, malesuada quis neque. Maecenas vel urna a velit pretium fermentum. Donec tortor enim,
tempor venenatis egestas a, tempor sed ipsum. Ut arcu justo, faucibus non imperdiet ac, interdum at diam. Pellentesque ipsum enim, venenatis ut iaculis vitae,
varius vitae sem. Sed rutrum quam ac elit euismod bibendum. Donec ultricies ultricies magna, at lacinia libero mollis aliquam. Sed ac arcu in tortor elementum
tincidunt vel interdum sem. Curabitur eget erat arcu. Praesent eget eros leo. Nam magna enim, sollicitudin vehicula scelerisque in, vulputate ut libero.
Praesent varius tincidunt commodo}.split
def self.name
LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join ' '
end
end
end
# pre-compute the insert statements and fake data compilation,
# so the benchmarks below show the actual runtime for the execute
# method, minus the setup steps
today = Date.today
roles = %w(boss-man lowly-worker)
puts "Inserting #{RECORDS} users and exhibits..."
RECORDS.times do
User.create(
:created_at => today,
:name => ActiveRecord::Faker.name,
:role => roles.sample
)
end
puts "SIZES USING SYMBOL:"
puts Benchmark.measure {
groups = User.all.group_by(:role)
groups.values.each do |users|
users.size
end
}
puts "SIZES USING BLOCK:"
puts Benchmark.measure {
groups = User.all.group_by { |u| u.role }
groups.values.each do |users|
users.size
end
}
puts "EACH USING SYMBOL:"
puts Benchmark.measure {
groups = User.all.group_by(:role)
groups.values.each do |users|
users.each do |user|
end
end
}
puts "EACH USING BLOCK:"
puts Benchmark.measure {
groups = User.all.group_by { |u| u.role }
groups.values.each do |users|
users.each do |user|
end
end
}
require 'benchmark'
require File.expand_path('../../../load_paths', __FILE__)
require "active_record"
RECORDS = 20000
conn = { :adapter => 'sqlite3', :database => ':memory:' }
ActiveRecord::Base.establish_connection(conn)
class User < ActiveRecord::Base
connection.create_table :users, :force => true do |t|
t.string :name, :role
t.timestamps
end
end
puts 'Generating data...'
module ActiveRecord
class Faker
LOREM = %Q{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse non aliquet diam. Curabitur vel urna metus, quis malesuada elit.
Integer consequat tincidunt felis. Etiam non erat dolor. Vivamus imperdiet nibh sit amet diam eleifend id posuere diam malesuada. Mauris at accumsan sem.
Donec id lorem neque. Fusce erat lorem, ornare eu congue vitae, malesuada quis neque. Maecenas vel urna a velit pretium fermentum. Donec tortor enim,
tempor venenatis egestas a, tempor sed ipsum. Ut arcu justo, faucibus non imperdiet ac, interdum at diam. Pellentesque ipsum enim, venenatis ut iaculis vitae,
varius vitae sem. Sed rutrum quam ac elit euismod bibendum. Donec ultricies ultricies magna, at lacinia libero mollis aliquam. Sed ac arcu in tortor elementum
tincidunt vel interdum sem. Curabitur eget erat arcu. Praesent eget eros leo. Nam magna enim, sollicitudin vehicula scelerisque in, vulputate ut libero.
Praesent varius tincidunt commodo}.split
def self.name
LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join ' '
end
end
end
# pre-compute the insert statements and fake data compilation,
# so the benchmarks below show the actual runtime for the execute
# method, minus the setup steps
today = Date.today
roles = %w(boss-man lowly-worker)
puts "Inserting #{RECORDS} users and exhibits..."
RECORDS.times do
User.create(
:created_at => today,
:name => ActiveRecord::Faker.name,
:role => roles.sample
)
end
puts "SIZES USING BLOCK:"
puts Benchmark.measure {
groups = User.all.group_by { |u| u.role }
groups.values.each do |users|
users.size
end
}
puts "EACH USING BLOCK:"
puts Benchmark.measure {
groups = User.all.group_by { |u| u.role }
groups.values.each do |users|
users.each do |user|
end
end
}
$ git checkout master
$ ruby activerecord/examples/group_by_old.rb
Generating data...
Inserting 20000 users and exhibits...
SIZES USING BLOCK:
0.700000 0.010000 0.710000 ( 0.722346)
EACH USING BLOCK:
0.680000 0.000000 0.680000 ( 0.700059)
$ git checkout group_by-B
$ ruby activerecord/examples/group_by_new.rb
Generating data...
Inserting 20000 users and exhibits...
SIZES USING SYMBOL:
0.030000 0.000000 0.030000 ( 0.070316)
SIZES USING BLOCK:
0.620000 0.020000 0.640000 ( 0.660111)
EACH USING SYMBOL:
0.620000 0.010000 0.630000 ( 0.631824)
EACH USING BLOCK:
0.690000 0.000000 0.690000 ( 0.687753)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.