Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 531 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String concatenation in Ruby

#11
Here's another benchmark inspired by [this gist](). It compares concatenation (`+`), appending (`<<`) and interpolation (`#{}`) for dynamic and predefined strings.

<!-- language: lang-rb -->

require 'benchmark'

# we will need the CAPTION and FORMAT constants:
include Benchmark

count = 100_000


puts "Dynamic strings"

Benchmark.benchmark(CAPTION, 7, FORMAT) do |bm|
bm.report("concat") { count.times { 11.to_s + '/' + 12.to_s } }
bm.report("append") { count.times { 11.to_s << '/' << 12.to_s } }
bm.report("interp") { count.times { "#{11}/#{12}" } }
end


puts "\nPredefined strings"

s11 = "11"
s12 = "12"
Benchmark.benchmark(CAPTION, 7, FORMAT) do |bm|
bm.report("concat") { count.times { s11 + '/' + s12 } }
bm.report("append") { count.times { s11 << '/' << s12 } }
bm.report("interp") { count.times { "#{s11}/#{s12}" } }
end

output:

<!-- language: lang-none -->

Dynamic strings
user system total real
concat 0.050000 0.000000 0.050000 ( 0.047770)
append 0.040000 0.000000 0.040000 ( 0.042724)
interp 0.050000 0.000000 0.050000 ( 0.051736)

Predefined strings
user system total real
concat 0.030000 0.000000 0.030000 ( 0.024888)
append 0.020000 0.000000 0.020000 ( 0.023373)
interp 3.160000 0.160000 3.320000 ( 3.311253)

Conclusion: interpolation in MRI is heavy.
Reply

#12
Situation matters, for example:

# this will not work
output = ''

Users.all.each do |user|
output + "#{user.email}\n"
end
# the output will be ''
puts output

# this will do the job
output = ''

Users.all.each do |user|
output << "#{user.email}\n"
end
# will get the desired output
puts output

In the first example, concatenating with `+` operator will not update the `output` object,however, in the second example, the `<<` operator will update the `output` object with each iteration. So, for the above type of situation, `<<` is better.
Reply

#13
You can also use `%` as follows:

source = "#{ROOT_DIR}/%s/App.config" % project

This approach works with `'` (single) quotation mark as well.
Reply

#14
You can concatenate in string definition directly:

nombre_apellido = "#{customer['first_name']} #{customer['last_name']} #{order_id}"
Reply

#15
For your particular case you could also use `Array#join` when constructing file path type of string:

string = [ROOT_DIR, project, 'App.config'].join('/')]

This has a pleasant side effect of automatically converting different types to string:

['foo', :bar, 1].join('/')
=>"foo/bar/1"

Reply

#16
You may use `+` or `<<` operator, but in ruby `.concat` function is the most preferable one, as it is much faster than other operators. You can use it like.

source = "#{ROOT_DIR}/".concat(project.concat("/App.config"))
Reply

#17
For Puppet:

```
$username = 'lala'
notify { "Hello ${username.capitalize}":
withpath => false,
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through