At 02:38 AM 11/16/2005, you wrote:
>Hello,
>
>I have a strange optimization bug and I wonder wy, also I wonder if
>there are similar optimizations in Delphi that I need to know of. I
>explain with a peace of code:
>
>if Foo.CheckForChanges then
>   Foo.SaveToFile;
>
>procedure TFoo.CheckForChanges: boolean;
>var
>   n: integer;
>begin
>   Result := False;
>   for n := 0 to Count - 1 do
>     Result := Result or CheckAccount(Items[n]);
>end;
>
>What happens here is that as soon as 1 CheckAccount returns True then it
>stop calling CheckAccount. I solved this with increment a counter for
>each CheckAccount and Result := Counter > 0; But this kind of
>optimization is for me a bug, or is it not ?  I discoverd this in D7,
>dont know about previous D versions, however I'm pretty sure I did
>similar things in other projects.

With short-circuit boolean evaluation, Delphi doesn't guarantee that 
each sub-expression will be evaluated.  If your intention is just to 
decide whether "Result" should be true or false, then it doesn't 
matter.  However, it sounds like you want CheckAccount to be called 
regardless.  Then just do this:

   for n := 0 to Count - 1 do
     if CheckAccount(Items[n]) then result := true;

That is, change the code from "I want to set Result" to "I want to 
run CheckAccount, and as a side effect set Result".

Although it is possible to turn off short-circuit evaluation as 
others suggested, I find that at least for the programs that I write 
(compute-intensive vs. user interaction intensive), there is a 
difference in performance.  YMMV.  :-)

>---
>Rgds, Wilfried
>http://www.mestdagh.biz
>
>__________________________________________________
>Delphi-Talk mailing list -> [email protected]
>http://www.elists.org/mailman/listinfo/delphi-talk

Regards,
Sid Gudes
PIA Systems Corporation
[EMAIL PROTECTED] 


__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to