455,331 Members | 1,278 Online
Need help? Post your question and get tips & solutions from a community of 455,331 IT Pros & Developers. It's quick & easy.

# about performace between ++i and i++

 P: n/a Hello!! Which have best performance between i++ or ++i. Is it exact the same or is it some very small difference in performace betwwwn these two. //Tony May 21 '06 #1
19 Replies

 P: n/a "Tony Johansson" a écrit dans le message de news: bu***************@newsb.telia.net... | Which have best performance between i++ or ++i. | Is it exact the same or is it some very small difference in performace | betwwwn these two. What's the point of comparing performance on two totally different operators ? If you want to increment the value after using it, then use i++. If you want to increment the value before using it, then use ++i. Performance really isn't the issue here. Joanna -- Joanna Carter [TeamB] Consultant Software Engineer May 21 '06 #2

 P: n/a "Tony Johansson" wrote: Which have best performance between i++ or ++i. Is it exact the same or is it some very small difference in performace betwwwn these two. Obviously, when used in an expression whose value is used, these have different semantics, so I won't talk about that. As freestanding expression statements, they are compiled to the following IL, respectively, where 'i' is the one and only local variable: i++; ldloc.0 ldc.i4.1 add stloc.0 ++i; ldloc.0 ldc.i4.1 add stloc.0 i = i + 1; ldloc.0 ldc.i4.1 add stloc.0 i += 1; ldloc.0 ldc.i4.1 add stloc.0 So, there is no difference. -- Barry -- http://barrkel.blogspot.com/ May 21 '06 #3

 P: n/a Hello!! That was the answer I wanted and not the answer I recived from Joanna //Tony "Barry Kelly" skrev i meddelandet news:jc********************************@4ax.com... "Tony Johansson" wrote: Which have best performance between i++ or ++i. Is it exact the same or is it some very small difference in performace betwwwn these two. Obviously, when used in an expression whose value is used, these have different semantics, so I won't talk about that. As freestanding expression statements, they are compiled to the following IL, respectively, where 'i' is the one and only local variable: i++; ldloc.0 ldc.i4.1 add stloc.0 ++i; ldloc.0 ldc.i4.1 add stloc.0 i = i + 1; ldloc.0 ldc.i4.1 add stloc.0 i += 1; ldloc.0 ldc.i4.1 add stloc.0 So, there is no difference. -- Barry -- http://barrkel.blogspot.com/ May 21 '06 #4

 P: n/a Hello!! I just wonder this person Berry is saying that i++ and ++i is compiled into the same IL code and therefore I wonder if it's possible for me to see this for myself. I'm sure he is right but I just wonder if I can see it also in any way. //Tony "Barry Kelly" skrev i meddelandet news:jc********************************@4ax.com... "Tony Johansson" wrote: Which have best performance between i++ or ++i. Is it exact the same or is it some very small difference in performace betwwwn these two. Obviously, when used in an expression whose value is used, these have different semantics, so I won't talk about that. As freestanding expression statements, they are compiled to the following IL, respectively, where 'i' is the one and only local variable: i++; ldloc.0 ldc.i4.1 add stloc.0 ++i; ldloc.0 ldc.i4.1 add stloc.0 i = i + 1; ldloc.0 ldc.i4.1 add stloc.0 i += 1; ldloc.0 ldc.i4.1 add stloc.0 So, there is no difference. -- Barry -- http://barrkel.blogspot.com/ May 21 '06 #5

 P: n/a because : for ( int i=0 ; i wrote in message news:%2****************@TK2MSFTNGP02.phx.gbl... "Tony Johansson" a écrit dans le message de news: bu***************@newsb.telia.net... | Which have best performance between i++ or ++i. | Is it exact the same or is it some very small difference in performace | betwwwn these two. What's the point of comparing performance on two totally different operators ? If you want to increment the value after using it, then use i++. If you want to increment the value before using it, then use ++i. Performance really isn't the issue here. Joanna -- Joanna Carter [TeamB] Consultant Software Engineer May 21 '06 #6

 P: n/a "gerry" a écrit dans le message de news: Og**************@TK2MSFTNGP04.phx.gbl... | because : | | for ( int i=0 ; i

 P: n/a "Tony Johansson" a écrit dans le message de news: L%****************@newsb.telia.net... | That was the answer I wanted and not the answer I recived from Joanna Barry's answer cannot be true for the example where you are not simply using the operator in a for loop declaration. See my reply to Gerry. while (i < 10) console.WriteLine(++i); IL_0005: ldloc.0 IL_0006: ldc.i4.1 IL_0007: add IL_0008: dup IL_0009: stloc.0 while (i < 10) console.WriteLine(++i); IL_0005: ldloc.0 IL_0006: dup IL_0007: ldc.i4.1 IL_0008: add IL_0009: stloc.0 Joanna -- Joanna Carter [TeamB] Consultant Software Engineer May 21 '06 #8

 P: n/a Tony Johansson wrote: I just wonder this person Berry is saying that i++ and ++i is compiled into the same IL code and therefore I wonder if it's possible for me to see this for myself. I'm sure he is right but I just wonder if I can see it also in any way. Yup - use ildasm. -- Jon Skeet - http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too May 21 '06 #9

 P: n/a I don't understand why are dismissing the very common situations where the difference between these 2 operators is irrelevant and could very well be a significant performance concern ? in your opinion, given the 2 for statements below - which is the "correct operator for the job" and why ? "Joanna Carter [TeamB]" wrote in message news:%2****************@TK2MSFTNGP03.phx.gbl... "gerry" a écrit dans le message de news: Og**************@TK2MSFTNGP04.phx.gbl... | because : | | for ( int i=0 ; i

 P: n/a "gerry" a écrit dans le message de news: %2****************@TK2MSFTNGP04.phx.gbl... |I don't understand why are dismissing the very common situations where the | difference between these 2 operators is irrelevant and could very well be a | significant performance concern ? Tony's original post didn't say anything about the context in which he wished to use the operators in question. I must say, I wasn't aware of the for(...) case where the difference is optimised away and am struggling to think of any other case where the compiler pulls a similar trick. It was with the majority of situations where it does make a difference in mind that I replied as I did. | in your opinion, given the 2 for statements below - which is the "correct | operator for the job" and why ? The choice is simply decided by whether you want to use the value before or after you have incremented it. Joanna -- Joanna Carter [TeamB] Consultant Software Engineer May 21 '06 #11

 P: n/a "Joanna Carter [TeamB]" wrote: "Tony Johansson" a écrit dans le message de news: L%****************@newsb.telia.net... | That was the answer I wanted and not the answer I recived from Joanna Barry's answer cannot be true My answer, in total, is true, Joanna. There are qualifications at the start. I'll quote them again, in case you missed them: Obviously, when used in an expression whose value is used, these have different semantics, so I won't talk about that. As freestanding expression statements, they are compiled to the following IL, respectively, where 'i' is the one and only local variable: To reiterate, my answer applies to freestanding expression statements. -- Barry -- http://barrkel.blogspot.com/ May 21 '06 #12

 P: n/a "Joanna Carter [TeamB]" wrote: | in your opinion, given the 2 for statements below - which is the "correct | operator for the job" and why ? The choice is simply decided by whether you want to use the value before or after you have incremented it. One traditional answer in C++ is that '++i' should be used, because when 'i' is a user-defined type, the 'i++' operator must return a copy while '++i' can return a reference. Of course, the difference is pretty much negligible in real-world applications. That was why the question was asked, IMHO. -- Barry -- http://barrkel.blogspot.com/ May 21 '06 #13

 P: n/a "Joanna Carter [TeamB]" wrote: why I mentioned that the two operators (pre and post) do different things, They have different semantics only when the value of the expression is used. Since the operators also have side-effects, they are commonly used for their side effects alone. The only meaningful interpretation of the question, then, is when the value of the expression *isn't* used, and the expressions are used as expression statements, for their side-effects alone. *That* is why it *is* meaningful to talk about relative performances, because the side-effects are the *same*. In very naive implementation, these two operators could have quite different implementations. For example, if the compiler wasn't aware of the common idiom of using an pre or post increment expression for its side-effects alone, it might use this IL instead: ++i; ldloc.0 ldc.i4.1 add dup stloc.0 pop i++; ldloc.0 dup ldc.i4.1 add stloc.0 pop And then these instructions, with a very naive JIT, might compile to different machine code instructions, which might perform differently. -- Barry -- http://barrkel.blogspot.com/ May 21 '06 #14

 P: n/a Joanna Carter [TeamB] wrote: It was with the majority of situations where it does make a difference in mind that I replied as I did. Do you really use pre and post-increment operators *usually* in the context of a larger statement? I almost never do, because every time I do, it takes me longer to read the code afterwards than if I'd done: most_of_statement; i++; or i++; most_of_statement; The same is true for the majority of code I've read in open source projects, colleagues etc. -- Jon Skeet - http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too May 21 '06 #15

 P: n/a "Jon Skeet [C# MVP]" a écrit dans le message de news: MP************************@msnews.microsoft.com... | Do you really use pre and post-increment operators *usually* in the | context of a larger statement? I almost never do, because every time I | do, it takes me longer to read the code afterwards than if I'd done: | | most_of_statement; | i++; I can see how using i++ or ++i as a single statement would behave identically in as far as it achieved the same result and, not having bothered to check the IL, assumed that any performance difference would have to be so small as to be negligable. The only occasion I could envisage a difference would be if the order of value changing mattered, and on such an occasion, you might not have the luxury of "tweaking" performance due to the integrity of the algorithm. In either case, I really can't see any difference in performance being that important in the scheme of things. Joanna -- Joanna Carter [TeamB] Consultant Software Engineer May 21 '06 #16

 P: n/a "Barry Kelly" a écrit dans le message de news: 12********************************@4ax.com... | My answer, in total, is true, Joanna. There are qualifications at the | start. I'll quote them again, in case you missed them: Yup, missed that, never mind we all learn something new every day :-) Joanna -- Joanna Carter [TeamB] Consultant Software Engineer May 21 '06 #17

 P: n/a Joanna Carter [TeamB] wrote: "Jon Skeet [C# MVP]" a écrit dans le message de news: MP************************@msnews.microsoft.com... | Do you really use pre and post-increment operators *usually* in the | context of a larger statement? I almost never do, because every time I | do, it takes me longer to read the code afterwards than if I'd done: | | most_of_statement; | i++; I can see how using i++ or ++i as a single statement would behave identically in as far as it achieved the same result and, not having bothered to check the IL, assumed that any performance difference would have to be so small as to be negligable. The only occasion I could envisage a difference would be if the order of value changing mattered, and on such an occasion, you might not have the luxury of "tweaking" performance due to the integrity of the algorithm. In either case, I really can't see any difference in performance being that important in the scheme of things. That's certainly true - I was just interested in your view that the "majority of situations" consists of ones where the value is used so the choice of pre/post makes a difference. I'm always interested in different programming styles :) -- Jon Skeet - http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too May 22 '06 #18

 P: n/a "Jon Skeet [C# MVP]" a écrit dans le message de news: MP***********************@msnews.microsoft.com... | I was just interested in your view that the |"majority of situations" consists of ones where the value is used so | the choice of pre/post makes a difference. I'm always interested in | | different programming styles :) Yes, I suppose I'm one of those programmers who likes to make use of "most" of the language features available :-) I will use ? : where appropriate instead of an if..else as well. Maybe it's to do with thinking that the more cryptic the code, the nearer it is to assembler, which I never really got the hang of. ;~} Joanna -- Joanna Carter [TeamB] Consultant Software Engineer May 22 '06 #19

 P: n/a Joanna Carter [TeamB] wrote: "Jon Skeet [C# MVP]" a écrit dans le message de news: MP***********************@msnews.microsoft.com... | I was just interested in your view that the |"majority of situations" consists of ones where the value is used so | the choice of pre/post makes a difference. I'm always interested in | | different programming styles :) Yes, I suppose I'm one of those programmers who likes to make use of "most" of the language features available :-) If your programming tasks are trivial, use difficult techniques to make them challening. ;) I will use ? : where appropriate instead of an if..else as well. Maybe it's to do with thinking that the more cryptic the code, the nearer it is to assembler, which I never really got the hang of. ;~} If the code was hard to write, it should be hard to read. ;) The problem with the close-to-assembly theory is that simple code in many cases are more efficient than cryptic one-liners, and also that assenbly code is actually made up of a lot of simple operations. May 22 '06 #20

### This discussion thread is closed

Replies have been disabled for this discussion.