Solved

for loop is counting backwards

Posted on 2000-04-04
12
350 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
Comment Utility
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
Comment Utility
Of course I mean 're-compiled'   :o)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
:-) sometimes the optimizer is a bit silly
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Author Comment

by:knowlton
Comment Utility
Hmmmm....okay.  :)
0
 
LVL 5

Author Comment

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

Expert Comment

by:Epsylon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now