Jef*_* H. 72 ruby string-concatenation string-interpolation
我刚开始学习Ruby(第一次编程),并且有一个关于变量的基本语法问题,以及编写代码的各种方法.
克里斯派恩的"学会编程"教会我写一个像这样的基本程序......
num_cars_again= 2
puts 'I own ' + num_cars_again.to_s + ' cars.'
Run Code Online (Sandbox Code Playgroud)
这很好,但后来我偶然发现了ruby.learncodethehardway.com上的教程,并被教导写出像这样的精确程序......
num_cars= 2
puts "I own #{num_cars} cars."
Run Code Online (Sandbox Code Playgroud)
它们都输出相同的东西,但显然选项2是一个更短的方法.
是否有任何特殊原因我应该使用一种格式而不是另一种格式?
Phr*_*ogz 70
每当TIMTOWTDI(有多种方法)时,你应该寻找利弊.使用"字符串插值"(第二个)而不是"字符串连接"(第一个):
优点:
to_s你缺点:
to_s你(也许你认为你有一个字符串,to_s表示不是你想要的,并隐藏它不是一个字符串的事实)"来分隔您的字符串而不是'(可能您有使用习惯',或者之前使用该字符串键入字符串,以后才需要使用字符串插值)插值和连接都有自己的优点和缺点.下面我给出了一个基准,它清楚地说明了在哪里使用concatination以及在哪里使用插值.
require 'benchmark'
iterations = 1_00_000
firstname = 'soundarapandian'
middlename = 'rathinasamy'
lastname = 'arumugam'
puts 'With dynamic new strings'
puts '===================================================='
5.times do
Benchmark.bm(10) do |benchmark|
benchmark.report('concatination') do
iterations.times do
'Mr. ' + firstname + middlename + lastname + ' aka soundar'
end
end
benchmark.report('interpolaton') do
iterations.times do
"Mr. #{firstname} #{middlename} #{lastname} aka soundar"
end
end
end
puts '--------------------------------------------------'
end
puts 'With predefined strings'
puts '===================================================='
5.times do
Benchmark.bm(10) do |benchmark|
benchmark.report('concatination') do
iterations.times do
firstname + middlename + lastname
end
end
benchmark.report('interpolaton') do
iterations.times do
"#{firstname} #{middlename} #{lastname}"
end
end
end
puts '--------------------------------------------------'
end
Run Code Online (Sandbox Code Playgroud)
以下是基准测试结果
Without predefined strings
====================================================
user system total real
concatination 0.170000 0.000000 0.170000 ( 0.165821)
interpolaton 0.130000 0.010000 0.140000 ( 0.133665)
--------------------------------------------------
user system total real
concatination 0.180000 0.000000 0.180000 ( 0.180410)
interpolaton 0.120000 0.000000 0.120000 ( 0.125051)
--------------------------------------------------
user system total real
concatination 0.140000 0.000000 0.140000 ( 0.134256)
interpolaton 0.110000 0.000000 0.110000 ( 0.111427)
--------------------------------------------------
user system total real
concatination 0.130000 0.000000 0.130000 ( 0.132047)
interpolaton 0.120000 0.000000 0.120000 ( 0.120443)
--------------------------------------------------
user system total real
concatination 0.170000 0.000000 0.170000 ( 0.170394)
interpolaton 0.150000 0.000000 0.150000 ( 0.149601)
--------------------------------------------------
With predefined strings
====================================================
user system total real
concatination 0.070000 0.000000 0.070000 ( 0.067735)
interpolaton 0.100000 0.000000 0.100000 ( 0.099335)
--------------------------------------------------
user system total real
concatination 0.060000 0.000000 0.060000 ( 0.061955)
interpolaton 0.130000 0.000000 0.130000 ( 0.127011)
--------------------------------------------------
user system total real
concatination 0.090000 0.000000 0.090000 ( 0.092136)
interpolaton 0.110000 0.000000 0.110000 ( 0.110224)
--------------------------------------------------
user system total real
concatination 0.080000 0.000000 0.080000 ( 0.077587)
interpolaton 0.110000 0.000000 0.110000 ( 0.112975)
--------------------------------------------------
user system total real
concatination 0.090000 0.000000 0.090000 ( 0.088154)
interpolaton 0.140000 0.000000 0.140000 ( 0.135349)
--------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
结论
如果字符串已经定义并确定它们永远不会是nil使用concatination,则使用interpolation.使用适当的一个,这将导致比易于缩进的性能更好的性能.
| 归档时间: |
|
| 查看次数: |
27961 次 |
| 最近记录: |