Skip to content

Commit e97f913

Browse files
sshawJuanitoFatas
authored andcommitted
Add String#sub! vs String#gub! vs String#[]=
Closes #41
1 parent cf64ab7 commit e97f913

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,36 @@ Comparison:
732732
String#gsub: 516604.2 i/s - 3.60x slower
733733
```
734734

735+
##### `String#sub!` vs `String#gsub!` vs `String#[]=` [code](code/string/sub!-vs-gsub!-vs-[]=.rb)
736+
737+
```
738+
$ ruby -v code/string/sub\!-vs-gsub\!-vs-\[\]\=.rb
739+
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
740+
741+
Calculating -------------------------------------
742+
String#['string']= 74.512k i/100ms
743+
String#sub!'string' 52.801k i/100ms
744+
String#gsub!'string' 34.480k i/100ms
745+
String#[/regexp/]= 55.325k i/100ms
746+
String#sub!/regexp/ 45.770k i/100ms
747+
String#gsub!/regexp/ 27.665k i/100ms
748+
-------------------------------------------------
749+
String#['string']= 1.215M (± 6.2%) i/s - 6.110M
750+
String#sub!'string' 752.731k (± 6.2%) i/s - 3.749M
751+
String#gsub!'string' 481.183k (± 4.4%) i/s - 2.414M
752+
String#[/regexp/]= 840.615k (± 5.3%) i/s - 4.205M
753+
String#sub!/regexp/ 663.075k (± 7.8%) i/s - 3.295M
754+
String#gsub!/regexp/ 342.004k (± 7.5%) i/s - 1.715M
755+
756+
Comparison:
757+
String#['string']=: 1214845.5 i/s
758+
String#[/regexp/]=: 840615.2 i/s - 1.45x slower
759+
String#sub!'string': 752731.4 i/s - 1.61x slower
760+
String#sub!/regexp/: 663075.3 i/s - 1.83x slower
761+
String#gsub!'string': 481183.5 i/s - 2.52x slower
762+
String#gsub!/regexp/: 342003.8 i/s - 3.55x slower
763+
```
764+
735765
##### `attr_accessor` vs `getter and setter` [code](code/general/attr-accessor-vs-getter-and-setter.rb)
736766

737767
> https://www.omniref.com/ruby/2.2.0/files/method.h?#annotation=4081781&line=47
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "benchmark/ips"
2+
3+
URL = "http://www.thelongestlistofthelongeststuffatthelongestdomainnameatlonglast.com/wearejustdoingthistobestupidnowsincethiscangoonforeverandeverandeverbutitstilllookskindaneatinthebrowsereventhoughitsabigwasteoftimeandenergyandhasnorealpointbutwehadtodoitanyways.html"
4+
5+
def fast
6+
s = URL.dup
7+
s["http://"] = ""
8+
end
9+
10+
def slow_1
11+
s = URL.dup
12+
s.sub! "http://", ""
13+
end
14+
15+
def slow_2
16+
s = URL.dup
17+
s.gsub! "http://", ""
18+
end
19+
20+
def slow_3
21+
s = URL.dup
22+
s[%r{http://}] = ""
23+
end
24+
25+
def slow_4
26+
s = URL.dup
27+
s.sub! %r{http://}, ""
28+
end
29+
30+
def slow_5
31+
s = URL.dup
32+
s.gsub! %r{http://}, ""
33+
end
34+
35+
36+
Benchmark.ips do |x|
37+
x.report("String#['string']=") { fast }
38+
x.report("String#sub!'string'") { slow_1 }
39+
x.report("String#gsub!'string'") { slow_2 }
40+
x.report("String#[/regexp/]=") { slow_3 }
41+
x.report("String#sub!/regexp/") { slow_4 }
42+
x.report("String#gsub!/regexp/") { slow_5 }
43+
x.compare!
44+
end

0 commit comments

Comments
 (0)