Ruby on Rails' Method Benchmark Performance

泪湿孤枕 提交于 2019-12-13 04:26:51

问题


I wonder what the benchmark for few rails methods would look like. Anyone got a website that can run custom methods?:

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

Is there a way to test this sort of benchmark? How slow or fast is that iteration?


回答1:


You can use ruby benchmark module for this kind of test

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


来源:https://stackoverflow.com/questions/33934638/ruby-on-rails-method-benchmark-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!