Skip to content

Commit f56798b

Browse files
jonathanhefnerJuanitoFatas
authored andcommitted
Add String#start_with? vs String#[].==
Closes #112
1 parent cec53a2 commit f56798b

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ Enumerable#sort_by (Symbol#to_proc): 25916.1 i/s
545545
Of note, `to_proc` for 1.8.7 is considerable slower than the block format
546546

547547
```
548-
$ ruby -v code/enumerable/inject-sum-vs-block.rb
548+
$ ruby -v code/enumerable/inject-sum-vs-block.rb
549549
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin14]
550550
Warming up --------------------------------------
551551
inject symbol 1.893k i/100ms
@@ -884,6 +884,25 @@ Comparison:
884884
String#=~: 891124.1 i/s - 3.30x slower
885885
```
886886

887+
##### `String#start_with?` vs `String#[].==` [code](code/string/start_with-vs-substring-==.rb)
888+
889+
```
890+
$ ruby -v code/string/end-string-checking-match-vs-end_with.rb
891+
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
892+
893+
Calculating -------------------------------------
894+
String#start_with? 2.047M (± 4.5%) i/s - 10.242M in 5.015146s
895+
String#[0, n] == 711.802k (± 7.3%) i/s - 3.551M in 5.019543s
896+
String#[RANGE] == 651.751k (± 6.2%) i/s - 3.296M in 5.078772s
897+
String#[0...n] == 427.207k (± 5.7%) i/s - 2.136M in 5.019245s
898+
899+
Comparison:
900+
String#start_with?: 2046618.9 i/s
901+
String#[0, n] ==: 711802.3 i/s - 2.88x slower
902+
String#[RANGE] ==: 651751.2 i/s - 3.14x slower
903+
String#[0...n] ==: 427206.8 i/s - 4.79x slower
904+
```
905+
887906
##### `Regexp#===` vs `String#match` vs `String#=~` [code ](code/string/===-vs-=~-vs-match.rb)
888907

889908
> :warning: <br>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "benchmark/ips"
2+
3+
PREFIX = "_"
4+
STRINGS = (0..9).map{|n| "#{PREFIX if n.odd?}#{n}" }
5+
6+
START_WITH = STRINGS.each_index.map do |i|
7+
"STRINGS[#{i}].start_with?(PREFIX)"
8+
end.join(";")
9+
10+
EQL_USING_LENGTH = STRINGS.each_index.map do |i|
11+
# use `eql?` instead of `==` to prevent warnings
12+
"STRINGS[#{i}][0, PREFIX.length].eql?(PREFIX)"
13+
end.join(";")
14+
15+
RANGE = 0...PREFIX.length
16+
17+
EQL_USING_RANGE_PREALLOC = STRINGS.each_index.map do |i|
18+
# use `eql?` instead of `==` to prevent warnings
19+
"STRINGS[#{i}][RANGE].eql?(PREFIX)"
20+
end.join(";")
21+
22+
EQL_USING_RANGE = STRINGS.each_index.map do |i|
23+
# use `eql?` instead of `==` to prevent warnings
24+
"STRINGS[#{i}][0...PREFIX.length].eql?(PREFIX)"
25+
end.join(";")
26+
27+
Benchmark.ips do |x|
28+
x.report("String#start_with?", START_WITH)
29+
x.report("String#[0, n] ==", EQL_USING_LENGTH)
30+
x.report("String#[RANGE] ==", EQL_USING_RANGE_PREALLOC)
31+
x.report("String#[0...n] ==", EQL_USING_RANGE)
32+
x.compare!
33+
end

0 commit comments

Comments
 (0)