views:

187

answers:

3

Hi. 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?

+3  A: 

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
A: 

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
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