I want compare time of execution Post.all
and SELECT * FROM posts
(or some other statements) How can i get execution time of Post.all
?
You can use the built-in Benchmark
module:
timing = Benchmark.measure { Post.all }
The various attributes of the object returned (Benchmark::Tms) are provided here – note that they are all in seconds:
cstime
: System CPU time of childrencutime
: User CPU time of childrenlabel
: Labelreal
: Elapsed real timestime
: System CPU timetotal
: Total time, that is utime + stime + cutime + cstimeutime
: User CPU timeIf you want to compare multiple measurements, you can use Benchmark.bm
or Benchmark.bmbm
:
Benchmark.bm do |x|
x.report("Post.all") { Post.all }
x.report("Measurement.all") { Measurement.all }
end
This will report the time taken for each run, so you can compare them.