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
?
Asked
Active
Viewed 4.7k times
116

Bohdan
- 8,298
- 6
- 41
- 51
3 Answers
270
timing = Benchmark.measure { Post.all }
The various attributes of the object returned (Benchmark::Tms) are provided here.

Jeremy Baker
- 3,986
- 3
- 24
- 27

Shadwell
- 34,314
- 14
- 94
- 99
-
5I've found this post very useful: http://caseyscarborough.com/blog/2013/07/22/benchmarking-your-ruby-code/ – hernanvicente May 30 '15 at 06:33
-
@Bohdan Would it be better to add another answer or comment with reference to the benchmark-ips gem? You've done quite a big edit to the answer there which changes it significantly and rather reduces the appropriateness of the votes on it already. – Shadwell Jan 02 '17 at 13:01
-
@Bohdan Upvoted. You can always change the accepted answer too if the new answer has become more appropriate. – Shadwell Jan 05 '17 at 16:14
-
This works. I wanted to know the unit of time is seconds as well, found it https://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html. – Ankit Marothi Jun 14 '22 at 13:16
5
With benchmark-ips gem:
2.3.0 :001 > require 'benchmark/ips'
=> true
2.3.0 :002 > Benchmark.ips do |x|
2.3.0 :003 > x.report("add: ") { 1+2 }
2.3.0 :004?> x.report("div: ") { 1/2 }
2.3.0 :005?> x.report("iis: ") { 1/2.0 }
2.3.0 :006?> end
Warming up --------------------------------------
add: 280.299k i/100ms
div: 278.189k i/100ms
iis: 266.526k i/100ms
Calculating -------------------------------------
add: 11.381M (± 4.5%) i/s - 56.901M in 5.010669s
div: 9.879M (± 4.6%) i/s - 49.518M in 5.024084s
iis: 9.289M (± 4.2%) i/s - 46.376M in 5.001639s

Bohdan
- 8,298
- 6
- 41
- 51
0
Here is my version of how to measure performance automatically in rails console using gem: https://github.com/igorkasyanchuk/execution_time
It's showing similar information which you already see during requests.
Sample:
[METRICS] Completed in 908.3ms | Allocations: 2894 | ActiveRecord: 0.9ms (queries: 13)

Igor Kasyanchuk
- 766
- 6
- 12