Author Topic: [closed] Is case for string more optimized than if...then...else?  (Read 363 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1835
I found an AI replay that Lazarus compiler optimizes case statement of string, by first sorting the condition strings and then use binary search, so that least number of comparison is done. If this is true, this should show better performance than multiple if..then..else statements. Is this true?
« Last Edit: August 10, 2026, 06:39:38 am by egsuh »

dsiders

  • Hero Member
  • *****
  • Posts: 1694
Re: Is case for string more optimized than if...then...else?
« Reply #1 on: August 10, 2026, 04:25:40 am »
I found an AI replay that Lazarus compiler optimizes case statement of string, by first sorting the condition strings and then use binary search, so that least number of comparison is done. If this is true, this should show better performance than multiple if..then..else statements. Is this true?

The answer is in the docs. https://www.freepascal.org/docs-html/ref/refsu56.html

440bx

  • Hero Member
  • *****
  • Posts: 6586
Re: Is case for string more optimized than if...then...else?
« Reply #2 on: August 10, 2026, 04:28:46 am »
No, it is not.

At least the code FPC generates does not reflect that in any way.

Some AI models hallucinate wet dreams. ;)

NOTE: @dsiders beat me to it.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

egsuh

  • Hero Member
  • *****
  • Posts: 1835
Re: Is case for string more optimized than if...then...else?
« Reply #3 on: August 10, 2026, 06:39:17 am »
I found many errors from AI... For example, there is a statistical package called PSPP. It defines some functions, e.g. MRGROUP, but does not implement it. When I asked AI how to do some kind of work, AI suggested MRGOUP. And I said again, it seems it is not used. Only defined. Then AI replied, "Ah.. sorry. You are right. It is defined but it's only for systax compatibility"..

egsuh

  • Hero Member
  • *****
  • Posts: 1835
Re: [closed] Is case for string more optimized than if...then...else?
« Reply #4 on: August 11, 2026, 10:27:17 am »
So, in the cases like this, is TStringList with sort on the best option? I think it will be compiled with optimization to look for a key (i.e. name property).

 

TinyPortal © 2005-2018