Skip to content

Commit df633ee

Browse files
committed
Merge pull request #59 from schneems/schneems/string-equal-squigly
String#=~ faster than String#match
2 parents a3ca904 + 076b96b commit df633ee

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,29 @@ Comparison:
687687
String#=~: 854830.3 i/s - 3.32x slower
688688
```
689689

690+
##### `String#match` vs `String#=~` [code ](code/string/match-vs-=~.rb)
691+
692+
> :warning: <br>
693+
> Sometimes you cant replace `match` with `=~`, <br />
694+
> This is only useful for cases where you are checkin <br />
695+
> for a match and not using the resultant match object.
696+
697+
```
698+
$ ruby -v code/string/match-vs-=~.rb
699+
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
700+
Calculating -------------------------------------
701+
String#=~ 69.889k i/100ms
702+
String#match 66.715k i/100ms
703+
-------------------------------------------------
704+
String#=~ 1.854M (±12.2%) i/s - 9.155M
705+
String#match 1.594M (±11.0%) i/s - 7.939M
706+
707+
Comparison:
708+
String#=~: 1853861.7 i/s
709+
String#match: 1593971.6 i/s - 1.16x slower
710+
```
711+
712+
690713
##### `String#gsub` vs `String#sub` [code](code/string/gsub-vs-sub.rb)
691714

692715
```

code/string/match-vs-=~.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'benchmark/ips'
2+
3+
def fast
4+
"foo".freeze =~ /boo/
5+
end
6+
7+
def slow
8+
"foo".freeze.match(/boo/)
9+
end
10+
11+
Benchmark.ips do |x|
12+
x.report("String#=~") { fast }
13+
x.report("String#match") { slow }
14+
x.compare!
15+
end

0 commit comments

Comments
 (0)