Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created April 27, 2016 23:20
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 tenderlove/1b65c6cd0d6709c3af55e3b945ec88a7 to your computer and use it in GitHub Desktop.
Save tenderlove/1b65c6cd0d6709c3af55e3b945ec88a7 to your computer and use it in GitHub Desktop.
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