Created
May 2, 2012 00:03
-
-
Save joeyw/2572524 to your computer and use it in GitHub Desktop.
Benchmark shamazing refactors
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 'shamazing' | |
require 'benchmark/ips' | |
sha = '9c2cddfaedaea9689a22e376aa20191041554fe8' | |
class Shamazing | |
def self.refactored(sha) | |
sha. | |
downcase. | |
scan(/[a-f]+/). | |
max_by(&:length) | |
end | |
def self.downcase_last(sha) | |
sha. | |
scan(/[a-f]+/i). | |
sort{|a,b| b.length <=> a.length}. | |
first. | |
downcase | |
end | |
def self.combined(sha) | |
sha. | |
scan(/[a-f]+/i). | |
max_by(&:length). | |
downcase | |
end | |
end | |
Benchmark.ips do |x| | |
x.report('original') { Shamazing.string(sha) } | |
x.report('downcase last') { Shamazing.downcase_last(sha) } | |
x.report('max_by') { Shamazing.refactored(sha) } | |
x.report('dc last & max_by') { Shamazing.combined(sha) } | |
end | |
__END__ | |
Calculating ------------------------------------- | |
original 7082 i/100ms | |
downcase last 7356 i/100ms | |
max_by 7674 i/100ms | |
dc last & max_by 8031 i/100ms | |
------------------------------------------------- | |
original 86754.0 (±1.8%) i/s - 439084 in 5.062890s | |
downcase last 90368.8 (±2.0%) i/s - 456072 in 5.048858s | |
max_by 93838.9 (±2.5%) i/s - 475788 in 5.073710s | |
dc last & max_by 97433.5 (±1.4%) i/s - 489891 in 5.028998s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment