Created
May 19, 2016 00:51
-
-
Save SamSaffron/ad2434bde76056871ee0939fe54df9e0 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
#!/usr/bin/env ruby | |
$: << File.dirname(__FILE__)+'/lib' | |
require 'benchmark/ips' | |
require 'fast_blank' | |
class String | |
# active support implementation | |
def slow_blank? | |
/\A[[:space:]]*\z/ === self | |
end | |
def new_slow_blank? | |
empty? || !(/[[:^space:]]/ === self) | |
end | |
def ruby_24_blank? | |
/\A[[:space:]]*\z/.match?(self) | |
end | |
end | |
test_strings = [ | |
"", | |
"\r\n\r\n ", | |
"this is a test", | |
" 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 | |
this is a longer test" | |
] | |
$has_match = (//.respond_to? :match?) | |
test_strings.each do |s| | |
raise "failed on #{s.inspect}" if s.blank? != s.slow_blank? | |
raise "failed on #{s.inspect}" if s.blank? != s.new_slow_blank? | |
if $has_match | |
# disabled while //.match?"" returns nil (bug) | |
# raise "failed on #{s.inspect}" if s.blank? != s.ruby_24_blank? | |
end | |
end | |
test_strings.each do |s| | |
puts "\n================== Test String Length: #{s.length} ==================" | |
Benchmark.ips do |x| | |
x.report("Fast Blank") do |times| | |
i = 0 | |
while i < times | |
s.blank? | |
i += 1 | |
end | |
end | |
x.report("Fast ActiveSupport") do |times| | |
i = 0 | |
while i < times | |
s.blank_as? | |
i += 1 | |
end | |
end | |
x.report("Slow Blank") do |times| | |
i = 0 | |
while i < times | |
s.slow_blank? | |
i += 1 | |
end | |
end | |
x.report("New Slow Blank") do |times| | |
i = 0 | |
while i < times | |
s.new_slow_blank? | |
i += 1 | |
end | |
end | |
if $has_match | |
x.report("Ruby 2.4 match?") do |times| | |
i = 0 | |
while i < times | |
s.ruby_24_blank? | |
i += 1 | |
end | |
end | |
end | |
x.compare! | |
end | |
end | |
# ================== Test String Length: 0 ================== | |
# Calculating ------------------------------------- | |
# Fast Blank 113.770k i/100ms | |
# Fast ActiveSupport 116.284k i/100ms | |
# Slow Blank 46.705k i/100ms | |
# New Slow Blank 112.101k i/100ms | |
# Ruby 2.4 match? 93.092k i/100ms | |
# ------------------------------------------------- | |
# Fast Blank 18.535M (±11.0%) i/s - 91.357M | |
# Fast ActiveSupport 18.789M (±10.4%) i/s - 92.795M | |
# Slow Blank 1.022M (± 7.3%) i/s - 5.091M | |
# New Slow Blank 19.926M (± 9.8%) i/s - 98.537M | |
# Ruby 2.4 match? 10.868M (± 8.8%) i/s - 53.900M | |
# | |
# Comparison: | |
# New Slow Blank: 19925554.0 i/s | |
# Fast ActiveSupport: 18788918.1 i/s - 1.06x slower | |
# Fast Blank: 18534902.4 i/s - 1.08x slower | |
# Ruby 2.4 match?: 10868476.1 i/s - 1.83x slower | |
# Slow Blank: 1021987.5 i/s - 19.50x slower | |
# | |
# | |
# ================== Test String Length: 6 ================== | |
# Calculating ------------------------------------- | |
# Fast Blank 96.829k i/100ms | |
# Fast ActiveSupport 111.282k i/100ms | |
# Slow Blank 44.464k i/100ms | |
# New Slow Blank 68.832k i/100ms | |
# Ruby 2.4 match? 76.882k i/100ms | |
# ------------------------------------------------- | |
# Fast Blank 8.746M (± 7.6%) i/s - 43.476M | |
# Fast ActiveSupport 8.807M (± 7.5%) i/s - 43.845M | |
# Slow Blank 865.704k (± 6.6%) i/s - 4.313M | |
# New Slow Blank 2.394M (± 7.3%) i/s - 11.908M | |
# Ruby 2.4 match? 4.498M (± 6.6%) i/s - 22.450M | |
# | |
# Comparison: | |
# Fast ActiveSupport: 8807338.9 i/s | |
# Fast Blank: 8745689.7 i/s - 1.01x slower | |
# Ruby 2.4 match?: 4498359.7 i/s - 1.96x slower | |
# New Slow Blank: 2394093.5 i/s - 3.68x slower | |
# Slow Blank: 865704.4 i/s - 10.17x slower | |
# | |
# | |
# ================== Test String Length: 14 ================== | |
# Calculating ------------------------------------- | |
# Fast Blank 108.509k i/100ms | |
# Fast ActiveSupport 112.127k i/100ms | |
# Slow Blank 63.625k i/100ms | |
# New Slow Blank 38.425k i/100ms | |
# Ruby 2.4 match? 76.900k i/100ms | |
# ------------------------------------------------- | |
# Fast Blank 15.941M (± 9.0%) i/s - 78.886M | |
# Fast ActiveSupport 16.571M (± 8.7%) i/s - 82.077M | |
# Slow Blank 2.348M (± 9.9%) i/s - 11.643M | |
# New Slow Blank 951.381k (± 5.2%) i/s - 4.765M | |
# Ruby 2.4 match? 6.432M (± 5.5%) i/s - 32.067M | |
# | |
# Comparison: | |
# Fast ActiveSupport: 16570765.1 i/s | |
# Fast Blank: 15941098.1 i/s - 1.04x slower | |
# Ruby 2.4 match?: 6432466.4 i/s - 2.58x slower | |
# Slow Blank: 2348113.3 i/s - 7.06x slower | |
# New Slow Blank: 951380.6 i/s - 17.42x slower | |
# | |
# | |
# ================== Test String Length: 24 ================== | |
# Calculating ------------------------------------- | |
# Fast Blank 113.828k i/100ms | |
# Fast ActiveSupport 115.010k i/100ms | |
# Slow Blank 69.520k i/100ms | |
# New Slow Blank 43.616k i/100ms | |
# Ruby 2.4 match? 70.745k i/100ms | |
# ------------------------------------------------- | |
# Fast Blank 10.181M (±10.0%) i/s - 50.312M | |
# Fast ActiveSupport 10.704M (±10.3%) i/s - 52.905M | |
# Slow Blank 2.000M (± 6.6%) i/s - 10.011M | |
# New Slow Blank 910.198k (± 7.0%) i/s - 4.536M | |
# Ruby 2.4 match? 4.125M (± 7.1%) i/s - 20.516M | |
# | |
# Comparison: | |
# Fast ActiveSupport: 10703759.1 i/s | |
# Fast Blank: 10180744.9 i/s - 1.05x slower | |
# Ruby 2.4 match?: 4125002.8 i/s - 2.59x slower | |
# Slow Blank: 2000197.5 i/s - 5.35x slower | |
# New Slow Blank: 910198.2 i/s - 11.76x slower | |
# | |
# | |
# ================== Test String Length: 136 ================== | |
# Calculating ------------------------------------- | |
# Fast Blank 108.669k i/100ms | |
# Fast ActiveSupport 113.360k i/100ms | |
# Slow Blank 61.259k i/100ms | |
# New Slow Blank 42.269k i/100ms | |
# Ruby 2.4 match? 79.272k i/100ms | |
# ------------------------------------------------- | |
# Fast Blank 10.282M (±10.7%) i/s - 50.748M | |
# Fast ActiveSupport 11.306M (± 8.0%) i/s - 56.113M | |
# Slow Blank 2.122M (± 6.3%) i/s - 10.598M | |
# New Slow Blank 1.037M (± 4.8%) i/s - 5.199M | |
# Ruby 2.4 match? 4.419M (± 6.3%) i/s - 22.038M | |
# | |
# Comparison: | |
# Fast ActiveSupport: 11305782.5 i/s | |
# Fast Blank: 10281574.6 i/s - 1.10x slower | |
# Ruby 2.4 match?: 4419110.0 i/s - 2.56x slower | |
# Slow Blank: 2121790.2 i/s - 5.33x slower | |
# New Slow Blank: 1036703.8 i/s - 10.91x slower | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment