Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

benchmarks for map/inject vs SQL sum. See the sample app here: https://github.com/srt32/benchmarks_example

View benchmark_funds.rake
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
desc 'Benchmark funds_raised methods'
 
ITERATIONS = 1000
 
task benchmark_sql: :environment do
campaign = Campaign.last
 
Benchmark.bm do |bm|
bm.report('sql') do
ITERATIONS.times do
Campaign.uncached do
campaign.funds_raised_sql
campaign.reload
end
end
end
end
end
 
 
task benchmark_ruby: :environment do
campaign = Campaign.last
 
Benchmark.bm do |bm|
bm.report('ruby') do
ITERATIONS.times do
Campaign.uncached do
campaign.funds_raised_ruby
campaign.reload
end
end
end
end
end
 
 
## Sample results for 1000 iterations
#
# user system total real
# ruby 17.660000 1.330000 18.990000 ( 20.545473)
# sql 0.690000 0.070000 0.760000 ( 1.045379)
View benchmark_funds.rake
1 2 3 4 5 6 7 8 9 10 11
class Campaign < ActiveRecord::Base
has_many :loans
 
def funds_raised_sql
loans.sum(:amount)
end
 
def funds_raised_ruby
loans.map(&:amount).inject(:+)
end
end
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.