Subj : Re: which for loop is better? To : comp.programming.threads From : Elie De Brauwer Date : Sat Mar 26 2005 04:11 pm 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 ? .