Skip to content

Instantly share code, notes, and snippets.

@schneems

schneems/.rb Secret

Created April 29, 2016 13:46
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 schneems/0c9b81e1542f457092c8bdaeacb01312 to your computer and use it in GitHub Desktop.
Save schneems/0c9b81e1542f457092c8bdaeacb01312 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
LONG_STRING = "
"
NEW_REGEX = /[[:^space:]]/
OLD_REGEX = /\A[[:space:]]*\z/
def new_blank(str)
str.empty? || !(NEW_REGEX === str)
end
def old_blank(str)
str.empty? || OLD_REGEX === str
end
Benchmark.ips do |x|
x.report('old') do |times|
i = 0
while i < times
old_blank(LONG_STRING)
i += 1
end
end
x.report('new') do |times|
i = 0
while i < times
new_blank(LONG_STRING)
i += 1
end
end
x.compare!
end
# Warming up --------------------------------------
# old 27.244k i/100ms
# new 19.854k i/100ms
# Calculating -------------------------------------
# old 317.945k (±11.0%) i/s - 1.580M in 5.032018s
# new 217.889k (±12.4%) i/s - 1.072M in 5.001483s
# Comparison:
# old: 317944.9 i/s
# new: 217889.3 i/s - 1.46x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment