-
-
Save schneems/0c9b81e1542f457092c8bdaeacb01312 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 = " | |
" | |
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