-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathstart_with-vs-substring-==.rb
More file actions
executable file
·40 lines (32 loc) · 1.2 KB
/
Copy pathstart_with-vs-substring-==.rb
File metadata and controls
executable file
·40 lines (32 loc) · 1.2 KB
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
require "benchmark/ips"
PREFIX = "_"
STRINGS = (0..9).map{|n| "#{PREFIX if n.odd?}#{n}" }
START_WITH = STRINGS.each_index.map do |i|
"STRINGS[#{i}].start_with?(PREFIX)"
end.join(";")
EQL_USING_LENGTH = STRINGS.each_index.map do |i|
# use `eql?` instead of `==` to prevent warnings
"STRINGS[#{i}][0, PREFIX.length].eql?(PREFIX)"
end.join(";")
RANGE = 0...PREFIX.length
EQL_USING_RANGE_PREALLOC = STRINGS.each_index.map do |i|
# use `eql?` instead of `==` to prevent warnings
"STRINGS[#{i}][RANGE].eql?(PREFIX)"
end.join(";")
EQL_USING_RANGE = STRINGS.each_index.map do |i|
# use `eql?` instead of `==` to prevent warnings
"STRINGS[#{i}][0...PREFIX.length].eql?(PREFIX)"
end.join(";")
# Each check is written out for all 10 strings (not looped), so only the checks are measured.
# The methods are defined from those strings.
eval "def fastest\n#{START_WITH}\nend"
eval "def faster\n#{EQL_USING_LENGTH}\nend"
eval "def fast\n#{EQL_USING_RANGE_PREALLOC}\nend"
eval "def slow\n#{EQL_USING_RANGE}\nend"
Benchmark.ips do |x|
x.report("String#start_with?") { fastest }
x.report("String#[0, n] ==") { faster }
x.report("String#[RANGE] ==") { fast }
x.report("String#[0...n] ==") { slow }
x.compare!
end