Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

for loop is counting backwards

Posted on 2000-04-04
12
Medium Priority
?
363 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 200 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
Technology Partners: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

722 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