Last active
May 8, 2016 23:59
-
-
Save bartoszkopinski/8049510 to your computer and use it in GitHub Desktop.
Mongoid benchmark, #find vs. #where, #only vs. no #only
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
n = 1_000 | |
id = User.first.id | |
profile = User.only(:profile).find(id).profile | |
Benchmark.bm(50) do |x| | |
x.report('User.find(id).profile') do | |
n.times{ User.find(id).profile } | |
end | |
x.report('User.find_by(id: id).profile') do | |
n.times{ User.find_by(id: id).profile } | |
end | |
x.report('User.where(id: id).first.profile') do | |
n.times{ User.where(id: id).first.profile } | |
end | |
x.report('User.only(:profile).find(id).profile') do | |
n.times{ User.only(:profile).find(id).profile } | |
end | |
x.report('User.where(id: id).only(:profile).first.profile') do | |
n.times{ User.where(id: id).only(:profile).first.profile } | |
end | |
end |
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
user system total real | |
User.find(id).profile 1.230000 0.100000 1.330000 ( 1.524522) | |
User.find_by(id: id).profile 1.210000 0.090000 1.300000 ( 1.486113) | |
User.where(id: id).first.profile 1.250000 0.090000 1.340000 ( 1.537307) | |
User.only(:profile).find(id).profile 1.280000 0.100000 1.380000 ( 1.608061) | |
User.where(id: id).only(:profile).first.profile 1.190000 0.090000 1.280000 ( 1.476007) | |
[Finished in 14.8s] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment