Subj : Re: which for loop is better? To : comp.programming.threads From : Sean Burke Date : Sun Mar 27 2005 07:32 pm Elie De Brauwer writes: > Sean Burke wrote: > > ssam_iit@yahoo.com (sam) writes: > > > > > >>David Hopwood wrote in message news:... > >> > >>>sam wrote: > >>> > >>>>Hi, > >>>> which one of the following codes in C would you prefer to print > >>>>those "hello"s and why? > >>>>int i,j; > >>>>1) for(i=0;i<=99;i++) > >>>> for(j=0;j<=999;j++) > >>>> printf("hello"); > >>>> > >>>>2) for(i=0;i<=999;i++) > >>>> for(j=0;j<=99;j++) > >>>> printf("hello"); > >>> > >>>As if it matters. What a stupid question, teaching people to micro-optimize > >>>without thinking about whether it can possibly lead to any difference in > >>>perceptible performance. Whoever set this homework question should be > >>>ashamed of themselves. > > > > > >>I should've elaborated the problem. Here it goes : for the same number > >>of total iterations(100*1000 in this example) in nested-for-loops, is > >>it more effective to have the outer for loop iterate more than the > >>inner one (case 2), or the other way around (case 1)? > > > > > > > > The days when runtime was determined by the number of instructions > > executed have long since passed us by. On modern machines, performance > > is governed by cache misses. > > > > And please don't top post. > > > > But in this case (using a compiled, non interpreted language) won't the > number of instructions be the same ? You obviously weren't around in the days when for (i = n - 1; i >= 0; i--) was faster than for (i = 0; i < n; i++) -SEan .