Solved

for loop is counting backwards

Posted on 2000-04-04
12
353 Views
Last Modified: 2010-04-04
Consider the following code:

procedure TFormMain.ButtonReJARClick(Sender: TObject);
var
  counter1 : integer;
  FileFailed : boolean;
  temp1 : string;
  temp2 : string;
begin
  temp1 := PathToFileToAdd;
  temp2 := JARFileCandidates[0].GetFilePath;
  FileFailed := false;
  for counter1 := 2 to 40 do
  begin
    if JARFileCandidates[counter1].GetReplaceStatus = true then
      CopyFile(pchar(PathToFileToAdd), pchar(JARFileCandidates[counter1].GetFilePath), FileFailed);
  end;
end;

As I step through this code in the debugger, counter1 is STARTING at 40 and counting BACKWARDS but I am not using "down to" in my for loop.

Can anyone explain this strange behavior to me?

0
Comment
Question by:knowlton
  • 6
  • 5
12 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 50 total points
ID: 2685725
This is caused by the code optimizer. Turn it off in the Project Menu > Options > Compiler tab > Optimization.

Make sure that your code is re-compilered after you changed this option.


Cheers,

Epsylon.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2685732
Of course I mean 're-compiled'   :o)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2686335
:-) sometimes the optimizer is a bit silly
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Author Comment

by:knowlton
ID: 2687067
Epsylon:

Why do the other for loops function properly then?  What is it about this particular for loop that makes the optimizer "wig-out"????

Thanks.
0
 
LVL 5

Author Comment

by:knowlton
ID: 2687135
From Delphi Help:

The $O directive controls code optimization. In the {$O+} state, the compiler performs a number of code optimizations, such as placing variables in CPU registers, eliminating common subexpressions, and generating induction variables. In the
{$O-} state, all such optimizations are disabled.
Other than for certain debugging situations, you should never have a need to turn optimizations off. All optimizations performed by Delphi's Object Pascal compiler are guaranteed not to alter the meaning of a program. In other words, Delphi performs no "unsafe" optimizations that require special awareness by the programmer.

So, does this mean that these weird issues only arise during debugging, and the compiled .exe won't display the symptoms?

Tom
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2688242
The reason why this happens is that the number of loops is known at compile-time. The assembler code is faster when it counts back to zero. It saves one or 2 instructions this way. In very small loops it could improve the performance up to 50%.
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690099
Hmmmm....okay.  :)
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690104
What is considered to be a "small" for loop?
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2690314
When there is not much code inside the loop. Something like this:

for i := 0 to 100000 do
  buffer[i] := 0;
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690351
Ahhh...my for loop would fit under "small" then.  :)

Now, is it true that this "bug" only displays itself at debug time?  Would the final .exe have this behavior?

Tom
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2690429
Not really. Inside your loop CopyFile is called. The time that it costs even depends in some hardware.

yes, the final exe will have it too. But what does it matter. The optimizer is just very smart and tries to gain performance where it can.
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690479
I suppose this isn't really a big deal, except when I was testing, I knew that my data structure [JARFileCandidates] only had one item in it.  Due to optimization I had to count down through 999 items to get to the one that I wanted to test.

I know that I can set conditionals at debug time that will execute a for loop until the counter equals a certain number, so I guess I could have done that.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 191
code issue 8 133
Delphi: making a BW image transparent 10 41
firemonkey keyboard covers the controls 1 27
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question