-5

I was asked this question in an interview.Please provide the solution if you know about this thing.Is there any difference between

for(int i=0;i<=N;i++)

and

for(int i=N;i>=0;i--)

which runs faster and why?

PS: Please differentiate on the basis of performance and the way compiler takes it.I am not asking about the basic difference between postfix and prefix notation.

candied_orange
  • 7,036
  • 2
  • 28
  • 62
codepad
  • 80
  • 1
  • 13
  • 1
    What language? The above could be any of at least four that I can think of offhand. – T.J. Crowder Nov 30 '14 at 08:50
  • Check out the answer for the C language: http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i – MichaelLo Nov 30 '14 at 08:51
  • I have edited the question, otherwise i am using java – codepad Nov 30 '14 at 08:52
  • @codepad: Then put that information in the question. – T.J. Crowder Nov 30 '14 at 08:53
  • @MichaelLo I know the basic difference between i++ and ++i. I am just asking how the compiler takes it and is there any effect of this on performance of the code – codepad Nov 30 '14 at 08:54
  • 4
    My suggestion would be to write a simple function using one method, and then another using the second method, and then disassemble the file and see how the compiler is converting it to assembly language. You might also write two separate programs, each using one of the two methods, and profile them (I'm not sure the best way in java, but in C++ you could use boost::timer for simple profiling.) My suspicion is that, with a modern compiler, the difference will be negligible. – Amadeus Nov 30 '14 at 08:55
  • Just disassemble and look by yourself. What's the problem? – code monkey Nov 30 '14 at 09:05
  • @Amadeus +1 for suggesting writing a test and finding out for yourself. – Craig Tullis Nov 30 '14 at 09:09
  • @Amadeus i tried to look at the assembly language of the code, but not getting anything. Please tell me the basic difference between both of these, how they affect the performance. – codepad Nov 30 '14 at 09:24

2 Answers2

2

I think this has the potential to become a better question, if we had more info. You should specify what language you are referring to. If this is JavaScript or something else then you need to mention that.

Secondly, consider revising your question. I suggest asking under which situations one is more appropriate than the other.

Whatever the language, the difference between the two is a stark contrast. The first for-loop involves incrementing a variable until a maximum value is reached (metaphorically-speaking, kind of like a race to the top), while the second involves decrementing a variable until a certain minimum is reached (like a race to the bottom).

Incidentally, in some languages, such as PHP performance is faster in a loop increment expression if you write the increment or decrement operators on the left instead of on the right.

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • @MrLister why you all are only looking at that thing which is not mentioned. I am not asking about what will happen if N is volatile or not. The question mentioned asks just a simple thing, what is the difference between for loops when we go from N to 0 and from 0 to N. If you know anything then please give specific answer otherwise there is no need of these useless comments – codepad Nov 30 '14 at 09:30
  • They aren't "useless", you just don't care about the information. Other people may well care – Clive Nov 30 '14 at 09:42
  • 1
    @codepad Given the kind of answer you were looking for, I'd say this question is off-topic for SO and might be a better fit for Programmers.SE. – Mr Lister Nov 30 '14 at 14:55
0

This is an excellent interview question because any answer you give is likely to be wrong and more importantly be something you never previously thought seriously about.

The whole point is to throw you off your game. They want to see how you react when you're pushed into an area that you feel like you should be expert in yet find something about which you are not. Knowing the perfect answer to this question doesn't help you because they'll have 12 more questions lined up to throw you off.

What they want is to see how you respond to this situation. Do you make stuff up? Do you think about it carefully? Can you justify why it's not an important concern? Do you insist your way of looking at it is the only valid way? Do you listen when told of another way? Are you a pain to deal with if told to do it another way?

They will care so much more about the answers to this than whether or not you can save them a CPU clock tick.

But if it turns out you are an expert in this one dusty arcane corner you might earn a point.
If that is your real question then it's a duplicate of this question: Which of these pieces of code is faster in Java?

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • I don't understand what point of the question they are not getting.Higher the reputation you have on stackoverflow the higher you become dumb i think – codepad Nov 30 '14 at 10:10
  • @codepad The comments are not a good place to talk about this. If you like we can chat about it here: http://chat.stackoverflow.com/rooms/65895/difference-between-i-and-i as can anyone else who has something to say. – candied_orange Nov 30 '14 at 10:18