Never use shortstring
with functions, even in Delphi 7.
When used as a function result, a shortstring
full content (i.e. all characters) is always copied.
So it will be slower than using a plain string
variable, which will only increment the internal reference count of the string, without copying the chars.
Since most of the Delphi RTL/VCL libraries use string
, using shortstring
in your code will add a lot of conversions for each call of those methods, whereas it will not be the case when using plain string
. And when calling the Windows API, the delphi string
type is already zero-ended, so will avoid any allocation and conversion, which is necessary when using shortstring
.
Therefore, you have to know that shortstring
is slower in all versions of Delphi, especially if you use FastMM4 as memory manager: when using the shortstring
for any RTL/VCL/API calls, the Delphi compiler will in fact allocate some hidden temporary string
, therefore allocate a buffer from the heap... far from efficient: it is preferred to use a string right away!
If you think that string
is slow (due to the atomic reference counting or memory allocation which are not so multithread-friendly), do not use shortstring
, but other structures - see my answer https://stackoverflow.com/a/6076407/458259 And never optimize without profiling with a tool like http://delphitools.info/samplingprofiler/
For functions using integer
values, byte/word/cardinal/integer
does not make a speed difference. My habit is to use integer
whenever possible, which will fit the CPU register size. Only Int64
kind of value will be slower, under 32 bit, since it will use 2 registers or a temporary variable on stack.
Update: with newer versions of Delphi, you will have the "inline
" keyword, which may help a lot about performance of small functions returning such types. And since Delphi 2009, string is Unicode and shortstring
is deprecated, since it is fixed to the System Non-Unicode applications code page. Use shortstring
only if you know exactly what you are doing, and are able to use Alt-F2 and see the generated asm code, guessing which hidden RTL functions are called.