Created
April 28, 2016 14:03
-
-
Save schneems/330efedbe310e59ad2f0f3e35358d3d5 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
########### THIS BENCH DOES NOT USE METHODS ########### | |
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" | |
Benchmark.ips do |x| | |
x.report('old') do |times| | |
i = 0 | |
while i < times | |
LONG_STRING.empty? || /\A[[:space:]]*\z/ === LONG_STRING | |
i += 1 | |
end | |
end | |
x.report('new') do |times| | |
i = 0 | |
while i < times | |
LONG_STRING.empty? || !(/[[:^space:]]/ === LONG_STRING) | |
i += 1 | |
end | |
end | |
x.compare! | |
end | |
# Warming up -------------------------------------- | |
# old 97.254k i/100ms | |
# new 127.135k i/100ms | |
# Calculating ------------------------------------- | |
# old 1.836M (±10.9%) i/s - 9.142M in 5.040422s | |
# new 2.684M (±15.5%) i/s - 13.095M in 5.038331s | |
# Comparison: | |
# new: 2684299.7 i/s | |
# old: 1836420.8 i/s - 1.46x slower | |
########### THIS BENCH DOES USE METHODS & THE RESULTS ARE FLIPPED ########### | |
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 108.721k i/100ms | |
# new 64.769k i/100ms | |
# Calculating ------------------------------------- | |
# old 1.745M (±10.5%) i/s - 8.698M in 5.044805s | |
# new 869.410k (± 8.8%) i/s - 4.340M in 5.031931s | |
# Comparison: | |
# old: 1744606.1 i/s | |
# new: 869410.3 i/s - 2.01x slower | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment