Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
main
Breadcrumbs
fast-ruby
/
code
/
string
/
concatenation.rb
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
38 lines (31 loc) · 860 Bytes
main
Breadcrumbs
fast-ruby
/
code
/
string
/
concatenation.rb
Copy path
Top
File metadata and controls
Code
Blame
38 lines (31 loc) · 860 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
require 'benchmark/ips'
# Object counts are per call, measured on Ruby 3.4.
# 1 object: the result.
# Both of these are built from literals when the file is parsed, so they are close; which one wins varies by Ruby version.
def faster
"#{'foo'}#{'bar'}"
end
# 1 object: the result.
def fast
'foo' 'bar'
end
# 2 objects: 'foo' and 'bar'; 'foo' is changed in place.
def slow
'foo' << 'bar'
end
# 2 objects: 'foo' and 'bar'; 'foo' is changed in place.
def slower
'foo'.concat 'bar'
end
# 3 objects: 'foo', 'bar' and the new result.
def slowest
'foo' + 'bar'
end
Benchmark.ips do |x|
x.report('"#{\'foo\'}#{\'bar\'}"') { faster }
x.report('"foo" "bar"') { fast }
x.report('String#append') { slow }
x.report('String#concat') { slower }
x.report('String#+') { slowest }
x.compare!
end
You can’t perform that action at this time.