Popular Posts

Tuesday, March 29, 2011

Which Loop Is Faster?

A very basic programming puzzle is being asked in programming interviews since last few years. The question is that out of below two lops, which will run faster?

public class FasterLoop {

public static void main(String[] args) {
int k = 0, l = 0, i, j;
for (i = 0, l++; i < 10; i++, k++)
for (j = 0, l++; j < 100; j++, k++);
System.out.println(k + " " + l);
k = 0;
l = 0;
for (i = 0, l++; i < 100; i++, k++)
for (j = 0, l++; j < 10; j++, k++);
System.out.println(k + " " + l);
}
}
  o/p:    1010   11
            1100  101
Refer :http://tech-queries.blogspot.com/2010/09/which-loop-is-faster.html

No comments:

Post a Comment