-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathsub!-vs-gsub!-vs-[]=.rb
More file actions
43 lines (35 loc) · 934 Bytes
/
Copy pathsub!-vs-gsub!-vs-[]=.rb
File metadata and controls
43 lines (35 loc) · 934 Bytes
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
41
42
43
require "benchmark/ips"
URL = "http://www.thelongestlistofthelongeststuffatthelongestdomainnameatlonglast.com/wearejustdoingthistobestupidnowsincethiscangoonforeverandeverandeverbutitstilllookskindaneatinthebrowsereventhoughitsabigwasteoftimeandenergyandhasnorealpointbutwehadtodoitanyways.html"
def fastest
s = URL.dup
s["http://"] = ""
end
def faster
s = URL.dup
s[%r{http://}] = ""
end
def fast
s = URL.dup
s.sub! "http://", ""
end
def slow
s = URL.dup
s.sub! %r{http://}, ""
end
def slower
s = URL.dup
s.gsub! "http://", ""
end
def slowest
s = URL.dup
s.gsub! %r{http://}, ""
end
Benchmark.ips do |x|
x.report("String#['string']=") { fastest }
x.report("String#[/regexp/]=") { faster }
x.report("String#sub!'string'") { fast }
x.report("String#sub!/regexp/") { slow }
x.report("String#gsub!'string'") { slower }
x.report("String#gsub!/regexp/") { slowest }
x.compare!
end