Skip to content

Instantly share code, notes, and snippets.

@derekprior
Created January 8, 2016 17:46
Show Gist options
  • Save derekprior/a499d6ca3ae57d60a261 to your computer and use it in GitHub Desktop.
Save derekprior/a499d6ca3ae57d60a261 to your computer and use it in GitHub Desktop.
CONFIDENCE_THRESHOLD = 4
# A user with some attempts
user = User.find(12481)
Benchmark.ips do |x|
x.report("in ruby") do
Flashcard.
all.
map { |q| q.most_recent_attempt_for(user) }.
select { |a| a.confidence > 0 && a.confidence < CONFIDENCE_THRESHOLD }.
map(&:flashcard)
end
x.report("in views") do
LatestAttempt.by(user).
confidence_below(CONFIDENCE_THRESHOLD).
includes(:flashcard).
map(&:flashcard)
end
x.compare!
end
# Comparison:
# in views: 1793.6 i/s
# in ruby: 21.1 i/s - 85.11x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment