Ross, Using the local boolean variable is most efficient in terms of speed and code/data space - this is because in *MOST* cases the optimiser will use a CPU register to hold the local variable and this is not only faster than calling methods, it also generates less code (one extra call is generated), and being a register variable, there is no data storage.
However, the point I was trying to make is: It is rather pointless trying to optimise the code as it really doesn't matter which form you use - for 6 controls, the user isn't going to see any difference. You might be able to save a few bytes of code if you choose one technique over another, but once again, IMHO, that is hardly worth it. In this instance, I think it is better to write and comment the code so that the meaning is clearly represented. Sometimes optimised code isn't the easiest to read. Often it is one is tempted to try and optimise little snippets of code, but if you really want to tweak your program, it is better to run it inside a profiler. You will be amazed at the results. Often you will find that it is the database / file access or some inefficient routine in a loop that executes 2000 times that is slowing down your program. Having said that, it doesn't hurt to use good algorithms, even if the gains are not apparent. This way, when it *DOES* matter, at least, you know that your code will be fast. Dennis. ----- Original Message ----- From: "Ross Levis" <[EMAIL PROTECTED]> To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]> Sent: Friday, October 31, 2003 1:49 PM Subject: Re: [DUG]: Just wondering... > Hi Dennis (and others) > > So I presume you are suggesting the more efficient way is to repeat the > itemindex = 1 expression. > > Options is usually a radio group, but sometimes it's a checkbox and using > the checked property instead of itemindex = 1. > > Often it is more than 2 components I want enabled/disabled based on the > Options selection. There can be 6 for example, which is why I was > interested to know the best way of coding it. Not that the speed difference > would be noticeable but if one is more efficient than the other then why not > use it from now on. > > Is it more efficient to store the value in a local boolean variable before > applying to several components? > > Regards, > Ross. > > ----- Original Message ----- > From: "Dennis Chuah" <[EMAIL PROTECTED]> > To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]> > Sent: Friday, October 31, 2003 1:11 PM > Subject: Re: [DUG]: Just wondering... > > > > > > Actually, this is a misconception. The compiler cannot fold the code > > because Option.ItemIndex accesses a property, which translates to a method > > call, and therefore can have side-effects. (Yes folks, all property > > accesses are method calls, even if no accessor methods are defined and the > > property points directly at a variable.) So, if the optimiser were to > fold > > the code, you would only get one method call instead of two! The only > time > > when code is folder (and it is not even guaranteed), is when you directly > > access variables. > > > > ----- Original Message ----- > > From: "Rohit Gupta" <[EMAIL PROTECTED]> > > To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]> > > Sent: Friday, October 31, 2003 12:07 PM > > Subject: Re: [DUG]: Just wondering... > > > > > > > First because the compiler should detect it and fodl the code. > > > > > > Second because it uses less cpu cycles. > > > > > > :-) > > > > > > I normally use a local variable to hold teh value with a name that > > > tells the world what it means. Or > > > > > > if Option.ItemIndex = 1 > > > then begin > > > end > > > else begin > > > end > > > > > > > > > > > Which of the following codeis the most efficient/fastest to execute? > > > > > > > > Label1.Enabled := Option.ItemIndex = 1; > > > > Label2.Enabled := Option.ItemIndex = 1; > > > > > > > > or > > > > > > > > Label1.Enabled := Option.ItemIndex = 1; > > > > Label2.Enabled := Label1.Enabled; > > > > > > > > Thanks, > > > > Ross Levis. > > -------------------------------------------------------------------------- - > New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED] > Website: http://www.delphi.org.nz > To UnSub, send email to: [EMAIL PROTECTED] > with body of "unsubscribe delphi" > Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/ > --------------------------------------------------------------------------- New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED] Website: http://www.delphi.org.nz To UnSub, send email to: [EMAIL PROTECTED] with body of "unsubscribe delphi" Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
