Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 5th, 2014  |  syntax: Ruby  |  size: 0.46 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'benchmark'
  2.  
  3. n = 1_000_000
  4. Benchmark.bm(20) do |x|
  5.   x.report("chained methods:") do
  6.     n.times do
  7.       ['coconut', 'lemon', 'banana', 'apple'].select { |e| e.size > 5 }
  8.                                              .map { |e| e.upcase }
  9.     end
  10.   end
  11.  
  12.   x.report("only reduce:") do
  13.     n.times do
  14.       ['coconut', 'lemon', 'banana', 'apple'].reduce([]) do |acc, e|
  15.         acc << e.upcase if e.size > 5
  16.         acc
  17.       end
  18.     end
  19.   end
  20. end
clone this paste RAW Paste Data