0

Lets simply fantasize and talk about performance.

As I have read the article in about.delphi.com called performance programming, there was interesting paragraphs claiming that Case statement ( in fact I prefer calling it as structure ) is faster than If ; For is faster than While and Repeat, but While is the slowest loop operator. I probably understand why While is the slowest, but ... what about others.

Have you tested / played / experimented or even gained real performance boost if changed, for example, all IF statements to Cases where possible?

Also I would like to talk about other - modified - loop and if statement behaviors in Delphi IDE, but it would be another question.

Shall we start, ladies and gentleman?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
HX_unbanned
  • 583
  • 1
  • 15
  • 51

3 Answers3

3

It's very rare when the type of control structure/loop construct do matter. You can't possibly get any reasonable performance increase if you change, say, For loop to While loop. Rather, algorithms do matter.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
2

I doubt for will be slower in practice than while.

AFAIK, for evaluates the condition one time while while (no pun intended) evaluates the condition every time. Consider following statements

for i = 0 to GettingAmountOfUsersIsTakingALotOfTime do
begin
  ...
end;

i := 0;
while i <= GettingAmountOfUsersIsTakingALotOfTime do
begin
  ...
  Inc(I);
end;

The while statement will be magnitudes of times slower than the if statement.

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • yes, you are absolutely correct. I did write wrong - I thought the same, but somehow wrote different ;) Anyways - thank you for tip and illustration - I hope it will help othe begginers in Delphi programming ;) – HX_unbanned Jul 08 '09 at 08:40
1

This is the best response I've seen to questions like this.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135