0

What would be the fastest way to check if an AnsiString equals some other AnsiString?

Currently i am doing this to check if the string is equal:

if AnsiCompareStr(mystring, 'helloworld') = 0 then
  ShowMessage('equal');

Also what would be the fastest way to check if a AnsiString contains another AnsiString (not complete Check)?

For this i am using:

StrPos(mystring, 'world') <> nil then
  ShowMessage('contains'); 
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
MRSNAPS
  • 89
  • 1
  • 6
  • 1
    These questions are always a little pointless in my view. Optimisation usually involves detailed knowledge of the input data and the overall algortihm. We're missing the context. – David Heffernan Sep 10 '15 at 17:50
  • What's more your compiler is FPC. I tagged accordingly. – David Heffernan Sep 10 '15 at 17:51
  • 2
    im compiling with delphi seattle trial and i just want to compare basic ansistrings. i don't get the hate. – MRSNAPS Sep 10 '15 at 17:55
  • 2
    OK. I retagged. There's no hate. A question asking about perf really needs context. What sort of input data do you have. Is locale relevant? Why is this a bottleneck? Make a good question and we'll be excited to answer. – David Heffernan Sep 10 '15 at 18:05
  • 3
    There's no hate. This is an impossible to answer question, because it asks about optimizing something that a) you haven't shown how you identified it to be a performance issue, and b) you've failed to benchmark with the data you're using. Optimization requires profiling to identify performance issues, and then testing of various alternatives *using the representative data* so there's something to compare. You've provided none of that information, so there's no possibility of determining the *fastest way* other than opinion and speculation. Questions asking for opinion are inappropriate here. – Ken White Sep 10 '15 at 22:36
  • Perhaps the other question would be why you opted for ANSI. An odd choice. – David Heffernan Sep 11 '15 at 07:19

3 Answers3

1

The answer is: it depends.

It depends on the type of strings, how they look, how long they are, and on which platform you are.

For any string comparison function that anyone comes up with here, I can come up with a scenario where that function is not the fastest solution.

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
0

AFAIK CompareText() is faster than AnsiCompareStr(), but only handle ASCII characters.

If you want a faster test of contained text, check https://stackoverflow.com/a/1554544/458259

Community
  • 1
  • 1
Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • The user said compare 2 AnsiStrings, all of the answers here involve conversion to Unicode string, even the Ansi compare functions in modern delphi versions convert the ansi strings to strings. The only way to properly compare 2 Ansi strings in Delphi is to do it yourself, Ansichar by AnsiChar. – Andy k May 13 '19 at 10:13
  • @Andyk I don't think so. There are AnsiString versions of most string functions, which do not make conversion into UnicodeString, in `System.AnsiStrings`. For instance, the `CompareText()` function in `System.AnsiStrings` is very fast (at least under Win32), and without any conversion. – Arnaud Bouchez May 16 '19 at 15:28
0

Fastest case sensitive way is standard compare:

if Str1 = Str2 then (case sensitive) It's faster than CompareStr()

Or CompareText - case insensitive

alitrun
  • 1,127
  • 8
  • 14
  • This involves 2 casts from AnsiString to string, so it's probably not fast at all. – Andy k May 13 '19 at 10:10
  • 1
    @Andyk This is not exact: `if Str1=Str2 then` won't make any conversion (it will call internally _AStrCmp from System.pas`), nor will CompareText(), if you use the version from System.AnsiStrings. Check the asm using Alt-F2 before guessing. – Arnaud Bouchez May 16 '19 at 15:31