-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
bench.rb
107 lines (91 loc) · 2.12 KB
/
bench.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/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 n24_slow_blank?
empty? || !(/[[:^space:]]/.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",
"
"
]
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?
raise "failed on #{s.inspect}" if s.blank? != s.n24_slow_blank?
raise "failed on #{s.inspect}" if s.blank? != s.sttni_blank?
raise "failed on #{s.inspect}" if s.blank? != s.opt_sttni_blank?
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
x.report("New 2.4 Slow Blank") do |times|
i = 0
while i < times
s.n24_slow_blank?
i += 1
end
end
x.report("STTNI Blank") do |times|
i = 0
while i < times
s.sttni_blank?
i += 1
end
end
x.report("OPT STTNI Blank") do |times|
i = 0
while i < times
s.opt_sttni_blank?
i += 1
end
end
x.compare!
end
end