Ruby Rails' 方法基准性能

Ruby on Rails' Method Benchmark Performance

我想知道几个 rails 方法的基准是什么样的。谁有可以 运行 自定义方法的网站?:

User.count
#=> 1000000 (Let's say about that)

u = User.where(account_id: 5)
u.count
#=> 100000

u.map |a| a.account_id = 6 end

有没有办法测试这种基准?该迭代有多慢或多快?

您可以使用 ruby benchmark module 进行此类测试

require 'benchmark'
Benchmark.bm do |x|
  x.report { User.count }
  x.report { u = User.where(account_id: 5); u.count }
  x.report { u = User.where(account_id: 5); u.map |a| a.account_id = 6 end }
end