Skip to content

Commit e8ac915

Browse files
authored
Merge pull request #188 from NARKOZ/benchmark-casecmp
Update benchmark with `String#casecmp?` example
2 parents bd5af46 + e896d22 commit e8ac915

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,22 +1101,27 @@ Comparison:
11011101
String#dup: 3566485.7 i/s - 2.16x slower
11021102
```
11031103

1104-
##### `String#casecmp` vs `String#downcase + ==` [code](code/string/casecmp-vs-downcase-==.rb)
1104+
##### `String#casecmp` vs `String#casecmp?` vs `String#downcase + ==` [code](code/string/casecmp-vs-downcase-==.rb)
1105+
1106+
`String#casecmp?` is available on Ruby 2.4 or later.
1107+
Note that `String#casecmp` only works on characters A-Z/a-z, not all of Unicode.
11051108

11061109
```
11071110
$ ruby -v code/string/casecmp-vs-downcase-\=\=.rb
1108-
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
1109-
1111+
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
1112+
Warming up --------------------------------------
1113+
String#casecmp? 395.796k i/100ms
1114+
String#downcase + == 543.958k i/100ms
1115+
String#casecmp 730.028k i/100ms
11101116
Calculating -------------------------------------
1111-
String#downcase + == 101.900k i/100ms
1112-
String#casecmp 109.828k i/100ms
1113-
-------------------------------------------------
1114-
String#downcase + == 2.915M (± 5.4%) i/s - 14.572M
1115-
String#casecmp 3.708M (± 6.1%) i/s - 18.561M
1117+
String#casecmp? 3.687M (±10.9%) i/s - 18.602M in 5.158065s
1118+
String#downcase + == 5.017M (±11.3%) i/s - 25.022M in 5.089175s
1119+
String#casecmp 6.948M (± 6.0%) i/s - 35.041M in 5.062714s
11161120
11171121
Comparison:
1118-
String#casecmp: 3708258.7 i/s
1119-
String#downcase + ==: 2914767.7 i/s - 1.27x slower
1122+
String#casecmp: 6948231.0 i/s
1123+
String#downcase + ==: 5017089.5 i/s - 1.38x (± 0.00) slower
1124+
String#casecmp?: 3686650.7 i/s - 1.88x (± 0.00) slower
11201125
```
11211126

11221127
##### String Concatenation [code](code/string/concatenation.rb)

code/string/casecmp-vs-downcase-==.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
SLUG = 'ABCD'
44

5+
def slowest
6+
SLUG.casecmp?('abcd')
7+
end
8+
59
def slow
610
SLUG.downcase == 'abcd'
711
end
@@ -11,6 +15,7 @@ def fast
1115
end
1216

1317
Benchmark.ips do |x|
18+
x.report("String#casecmp?") { slowest } if RUBY_VERSION >= "2.4.0".freeze
1419
x.report('String#downcase + ==') { slow }
1520
x.report('String#casecmp') { fast }
1621
x.compare!

0 commit comments

Comments
 (0)