我在代码中有一些情况需要构建大量文本字符串,比如复杂的SQL语句。我打算将这些文本多次组合在一起,每次略有不同的参数。我已经养成了使用一个名为
或者我通常的做法是:
procedure A(const S: String);
的子程序的习惯,该子程序简单地将文本(S
)附加到较大的字符串上Text := Text + S + #10 + #13;
我想知道与传统的字符串连接相比,这样做是否会影响性能?我开始认为编译器会优化这样的操作:Text := 'some' + ' ' + 'text' + ' ' + 'and' + ' ' + 'such';
to
Text := 'some text and such';
这是真的吗?编译器会优化这种情况吗?如果是这样,我可能决定将所有内容更改为类似于这样的东西:
Text := 'select something from sometable st'+#10+#13+
'join someothertable sot on sot.id = st.sotid'+#10+#13+
'where sot.somevalue = 1'+#10+#13+
'order by sot.sorting';
这个理论上比...更快吗?
Text:= Text + 'select something from sometable st'+#10+#13;
Text:= Text + 'join someothertable sot on sot.id = st.sotid'+#10+#13;
Text:= Text + 'where sot.somevalue = 1'+#10+#13;
Text:= Text + 'order by sot.sorting';
或者我通常的做法是:
A('select something from sometable st');
A('join someothertable sot on sot.id = st.sotid');
A('where sot.somevalue = 1');
A('order by sot.sorting');