德尔福传递const字符串与传递var字符串的性能 [英] Delphi; performance of passing const strings versus passing var strings

查看:121
本文介绍了德尔福传递const字符串与传递var字符串的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快一个;我正确的想法是将字符串传递给方法作为CONST涉及到将字符串传递为VAR的更多开销?编译器将得到Delphi制作一个字符串的副本,然后传递副本,如果string参数被声明为CONST,对吗?



问题的原因有点乏味我们有一个传统的 Delphi 5 实用程序,其日期是真正的编号(替换正在开发中)。它执行大量的字符串处理,经常在各种功能和过程之间传递1-2Kb字符串。在整个代码中,已经遵守使用CONST或VAR传递参数(取决于手头的工作)的正确观察。我们只是寻找几个快速胜利,可能会在执行时间之后几秒钟的时间内消失,直到新版本准备就绪。我们认为将内存管理器从默认的Delphi 5更改为FastMM,我们也想知道是否值得改变字符串传递的方式 - 因为代码对于传递为const的字符串工作正常,我们不看到一个问题,如果我们将这些声明更改为var - 该方法中的代码不会更改字符串。



但真的会有真正的差异? (该程序实际上对这些1kb + ish字符串进行了大量处理,在高峰时段几分钟处理了几百个字符串)。在重写这些字符串中,这些字符串被保存在对象/类变量中,所以它们根本没有被真正复制/传递,但是在旧代码中,它是非常老派的帕斯卡。



自然地,我们将剖析整个程序的运行情况,看看我们做了什么差异,但如果我们对于如何做出明确的错误,实际上没有任何意义字符串传递在第一次实现!

解决方案

不,使用<$ c在你的情况下,$ c> const 或 var 在这两种情况下,都会传递一个指向该字符串的指针作为参数。如果参数是 const ,编译器不允许对其进行任何修改。请注意,如果您遇到困难,这并不排除对字符串的修改:

  procedure TForm1.Button1Click(Sender:TObject); 
var
s:string;
begin
s:='foo bar baz';
UniqueString(s);
SetConstCaption(s);
标题:= s;
结束

程序TForm1.SetConstCaption(const AValue:string);
var
P:PChar;
begin
P:= PChar(AValue);
P [3]:='?';
标题:= AValue;
结束

这将实际上改变调用方法中的本地字符串变量,证明只有一个指针是



但绝对使用FastMM4,它应该有更大的性能影响。


Quick one; am I right in thinking that passing a string to a method 'as a CONST' involves more overhead than passing a string as a 'VAR'? The compiler will get Delphi to make a copy of the string and then pass the copy, if the string parameter is declared as a CONST, right?

The reason for the question is a bit tedious; we have a legacy Delphi 5 utility whose days are truly numbered (the replacement is under development). It does a large amount of string processing, frequently passing 1-2Kb strings between various functions and procedures. Throughout the code, the 'correct' observation of using CONST or VAR to pass parameters (depending on the job in hand) has been adhered to. We're just looking for a few 'quick wins' that might shave a few microseconds off the execution time, to tide us over until the new version is ready. We thought of changing the memory manager from the default Delphi 5 one to FastMM, and we also wondered if it was worth altering the way the strings are passed around - because the code is working fine with the strings passed as const, we don't see a problem if we changed those declarations to var - the code within that method isn't going to change the string.

But would it really make any difference in real terms? (The program really just does a large amount of processing on these 1kb+ish strings; several hundred strings a minute at peak times). In the re-write these strings are being held in objects/class variables, so they're not really being copied/passed around in the same way at all, but in the legacy code it's very much 'old school' pascal.

Naturally we'll profile an overall run of the program to see what difference we've made but there's no point in actually trying this if we're categorically wrong about how the string-passing works in the first instance!

解决方案

No, there shouldn't be any performance difference between using const or var in your case. In both cases a pointer to the string is passed as the parameter. If the parameter is const the compiler simply disallows any modifications to it. Note that this does not preclude modifications to the string if you get tricky:

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := 'foo bar baz';
  UniqueString(s);
  SetConstCaption(s);
  Caption := s;
end;

procedure TForm1.SetConstCaption(const AValue: string);
var
  P: PChar;
begin
  P := PChar(AValue);
  P[3] := '?';
  Caption := AValue;
end;

This will actually change the local string variable in the calling method, proof that only a pointer to it is passed.

But definitely use FastMM4, it should have a much bigger performance impact.

这篇关于德尔福传递const字符串与传递var字符串的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆