Solved

for loop is counting backwards

Posted on 2000-04-04
12
360 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom 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:Tom 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:Tom Knowlton
ID: 2690099
Hmmmm....okay.  :)
0
 
LVL 5

Author Comment

by:Tom 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:Tom 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:Tom 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

628 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