[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: programing styles



Just for the record - in my experience we will have faster code if we 
also write extra line of code:

private void myproc(Vector vect){
  int z = vect.size();
  //this way we are avoiding function calls every iteration
  for( int i=z; i >= 0; i-- ){
    MyObject obj = (MyObject)vect.elementAt(i);
    //int y = (MyObject)vect.elementAt(i).getY();
    //there we are also avoiding extra function calls
    int y = obj.getY();
  }
}

Now about a subject - I like to have declarations inside the loop.

Best Regards

Jerzy Puchala

On Fri, 1 Nov 2002, Webb Morris wrote:

> I seem to recall, that for pure efficiency, the following would be "best":
> 
> private void myproc(Vector vect){
>   for( int i=vect.size(); i >= 0; i-- ){
>     MyObject obj = (MyObject)vect.elementAt(i);
>     int y = (MyObject)vect.elementAt(i).getY();
>   }
> }
> 
> By iterating over the list backwards you avoid having to recalculate the size of the list on every
> iteration.
> 
> WM
> 
> 
> --- "Gudavalli, Manidhar" <manidhar.gudavalli@eds.com> wrote:
> > 
> > 
> > 
> > HI
> > 
> > Which  style of coding is best ?
> > 
> > [1]
> > private void myproc(Vector vect){
> > for (i=1; i < vect.size() ; i++){
> > 
> > MyObject obj = (MyObject) vect.elementAt(i);
> >   int y = obj.getY();
> > }
> > }
> > 
> > or
> > [2]
> > private void myproc(Vector vect){
> > MyObject obj = null;
> > int y= 0;
> > 
> > for (i=1; i < vect.size() ; i++){
> > 
> >   obj = (MyObject) vect.elementAt(i);
> >     y = obj.getY();
> > }
> > }
> > 
> 
> 
> __________________________________________________
> Do you Yahoo!?
> HotJobs - Search new jobs daily now
> http://hotjobs.yahoo.com/
> 

-- 
+--------------------------------+
|         Jerzy Puchala          |
+--------------------------------+
|       jerzypuc@scdi.com        |
+--------------------------------+