sqlruby-on-railstimeconsole

How to get execution time in rails console?


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 ?


Solution

  • 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:

    If 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.