Fastest Parameter Passing in delphi 7?
Executive summary

Passing by const is the fastest for Delphi string types.

Details


function myfunction(data: string): boolean;

  
  Slow since the data must be copied to another location in memory and passed to myfunction


That's not what happens. In fact the caller does nothing more than pass the string variable, stored as a pointer, to the function. What happens in myfunction is that the reference count  to the string is incremented on entry and decremented on exit. This requires an implicit try/finally to be written into the code to ensure that the decrement happens in case of exceptions. The reference counting code and the try/finally consume time.

Note that the string contents are not copied at all because Delphi strings are optimised to use copy-on-write. You only pay the price of copying them if you modify the string.


function myfunction(var data: string): boolean;

  
  Faster since a pointer to the original data is passed to the function and data is never copied


This is pretty much equivalent to the previous by value code above. The parameter (i.e. the pointer to the string) is passed by reference rather than value, but otherwise there are no differences.


function myfunction(const data: string): boolean;



And now we come to the winner. Since the function is not allowed to modify the contents of the string, the compiler can omit the reference counting code and the try/finally. So this is the fastest of all.