Last active
December 31, 2015 17:49
-
-
Save akshay-vishnoi/8022922 to your computer and use it in GitHub Desktop.
Performance Increase in #many?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
def old_many?(enum) | |
cnt = 0 | |
enum.any? { (cnt += 1) > 1 } | |
end | |
def new_many?(enum) | |
enum.count > 1 | |
end | |
enum = (1..1000).to_a | |
Benchmark.ips do |b| | |
b.report('old') do | |
old_many? enum | |
end | |
b.report('new') do | |
new_many? enum | |
end | |
end | |
# Output | |
# Calculating ------------------------------------- | |
# old 57932 i/100ms | |
# new 93881 i/100ms | |
# ------------------------------------------------- | |
# old 1312923.5 (±3.1%) i/s - 6604248 in 5.035705s | |
# new 4991185.8 (±3.2%) i/s - 24972346 in 5.009269s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment