Skip to content

Instantly share code, notes, and snippets.

@almostwhitehat
Created February 25, 2015 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almostwhitehat/1722cd54c09d034274b5 to your computer and use it in GitHub Desktop.
Save almostwhitehat/1722cd54c09d034274b5 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
ARRAY = [*1..100]
def slow
ARRAY.select { |x| x.eql?(50) }.last
end
def fast
ARRAY.reverse.detect { |x| x.eql?(50) }
end
Benchmark.ips do |x|
x.report('slow') { slow }
x.report('fast') { fast }
x.compare!
end
@almostwhitehat
Copy link
Author

ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
Calculating -------------------------------------
                slow     8.011k i/100ms
                fast    11.829k i/100ms
-------------------------------------------------
                slow     87.054k (± 3.6%) i/s -    440.605k
                fast    135.431k (± 3.4%) i/s -    686.082k

Comparison:
                fast:   135430.6 i/s
                slow:    87053.8 i/s - 1.56x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment