如何在Rails控制台中获取执行时间?


104

我想比较执行时间Post.allSELECT * FROM posts(或其他一些语句)如何获得执行时间Post.all

Answers:


239
timing = Benchmark.measure { Post.all }

物体的各种属性返回(基准:: Tm值)提供了这里


5
我发现这个职位是非常有用的:caseyscarborough.com/blog/2013/07/22/...
hernanvicente

@Bohdan是否会参考基准IPs gem添加另一个答案或评论更好?您已经对答案做了很大的修改,从而极大地改变了答案,而已降低了对其投票的适当性。
Shadwell

@Bohdan赞成。如果新答案变得更加合适,您也可以随时更改接受的答案。
Shadwell

4

使用beta-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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.