Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2023-06

Passing around Pascal strings vs AnsiString

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

I was curious, if you had a Pascal (short) string and it was going to be passed around often would that be slower than using an AnsiString since its size is that of a pointer? I always prefer short strings when I know the length will be less than 255 but can copying that value between function calls add up to be more than the cost of allocating and reference counting the AnsiString?

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 6 lines
I was curious, if you had a Pascal (short) string and it was going to be passed around often would that be slower than using an AnsiString since its size is that of a pointer? I always prefer short strings when I know the length will be less than 255 but can copying that value between function calls add up to be more than the cost of allocating and reference counting the AnsiString?

For parameters that are passed by value, the copying most likely will be slower since all bytes from the string must be copied to the new string.

For var parameters it should not matter.

Michael.

Reply by email

Hairy Pixels
to Michael Van Canneyt
Show quoted text 6 lines
On Jun 14, 2023, at 8:30 PM, Michael Van Canneyt via fpc-pascal wrote: For parameters that are passed by value, the copying most likely will be slower since all bytes from the string must be copied to the new string. For var parameters it should not matter.

ah yes var. What about const? surely it behaves the same as var for short strings as they are a fundamental type.

Regards,
Ryan Joseph

Reply by email

Peter B
to Hairy Pixels: I was curious, if you had a Pascal (short) string and it was going to be passed around…
Hairy Pixels wrote 8 lines
I was curious, if you had a Pascal (short) string and it was going to be passed around often would that be slower than using an AnsiString since its size is that of a pointer? I always prefer short strings when I know the length will be less than 255 but can copying that value between function calls add up to be more than the cost of allocating and reference counting the AnsiString? Regards, Ryan Joseph _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Value parameter passing will be slower for shortstring, but anything else is likely to be much faster. Best to profile the app if concerned about this.

I recall many years ago, an application that ran about 50 times slower, following a Delphi upgrade. It turned out the issue was switching the string default from pascal shortstring to ansi.

YMMV, but I would always use shortsting locally where length permits.

Cheers,
Peter

P.S.  Think this was D4

Reply by email