-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compare gsub vs tr vs delete to remove character from string (#121)
- Loading branch information
1 parent
c12ede2
commit 1198345
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'benchmark/ips' | ||
|
||
WORDS = 'writing fast ruby' | ||
SPACE = ' ' | ||
|
||
def use_gsub | ||
WORDS.gsub(' ', '') | ||
end | ||
|
||
def use_tr | ||
WORDS.tr(' ', '') | ||
end | ||
|
||
def use_delete | ||
WORDS.delete(' ') | ||
end | ||
|
||
def use_delete_const | ||
WORDS.delete(SPACE) | ||
end | ||
|
||
Benchmark.ips do |x| | ||
x.report('String#gsub') { use_gsub } | ||
x.report('String#tr') { use_tr } | ||
x.report('String#delete') { use_delete } | ||
x.report('String#delete const') { use_delete_const } | ||
x.compare! | ||
end |