Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.40 KB  |  hits: 9  |  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. // Interestingly, the regular maxItem is twice as fast as this much more elegant version.
  2. // However, you can learn a lot by trying to solve and reimplement common methods with e.g. inject.
  3.  
  4.  
  5. f = {|a| a.inject(a.first, {|last, current|
  6.         max(current, last)
  7. })}
  8.  
  9.  
  10. // an example array
  11. a = {100.0.rand2}!1000
  12.  
  13. // evaluating the inject method
  14. f.(a)
  15.  
  16.  
  17. // benchmark
  18. {100.do{f.(a)}}.bench
  19. {100.do{a.maxItem}}.bench