Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. require 'benchmark'
  2. require 'active_support/all'
  3.  
  4. arr = Array.new(1000_0000) { rand(1...20) }
  5.  
  6. Benchmark.bm do |benchmark|
  7. benchmark.report "arr.sum { |n| n.odd? ? n : 0 }" do
  8. arr.sum { |n| n.odd? ? n : 0 }
  9. end
  10. benchmark.report "arr.select(&:odd?).sum" do
  11. arr.select(&:odd?).sum
  12. end
  13. benchmark.report "arr.select(&:odd?).sum" do
  14. arr.select{|a| a.odd?}.sum
  15. end
  16. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement