Created
April 27, 2016 23:20
-
-
Save tenderlove/1b65c6cd0d6709c3af55e3b945ec88a7 to your computer and use it in GitHub Desktop.
This file contains 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' | |
LONG_STRING = " this is a longer test | |
this is a longer test | |
this is a longer test | |
this is a longer test | |
this is a longer test" | |
class String | |
def original_blank? | |
/\A[[:space:]]*\z/ === self | |
end | |
def original_blank_with_empty? | |
empty? || (/\A[[:space:]]*\z/ === self) | |
end | |
def new_blank? | |
empty? || !(/[[:^space:]]/ === self) | |
end | |
end | |
def func string | |
string.empty? || !(/[[:^space:]]/ === string) | |
end | |
GC.disable | |
Benchmark.ips do |x| | |
x.report('new blank with empty while loop') do |t| | |
i = 0 | |
while i < t | |
func LONG_STRING | |
i += 1 | |
end | |
end | |
x.report('patched string new blank with while loop') do |t| | |
i = 0 | |
while i < t | |
LONG_STRING.new_blank? | |
i += 1 | |
end | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment