473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

about performace between ++i and i++

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 2246
"Tony Johansson" <jo*****************@telia.com> 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
"Tony Johansson" <jo*****************@telia.com> 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
Hello!!

That was the answer I wanted and not the answer I recived from Joanna

//Tony

"Barry Kelly" <ba***********@gmail.com> skrev i meddelandet
news:jc********************************@4ax.com...
"Tony Johansson" <jo*****************@telia.com> 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
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" <ba***********@gmail.com> skrev i meddelandet
news:jc********************************@4ax.com...
"Tony Johansson" <jo*****************@telia.com> 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
because :

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

in both cases the value is used after the operation is applied

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Tony Johansson" <jo*****************@telia.com> 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
"gerry" <ge**@nospam.nospam> a écrit dans le message de news:
Og**************@TK2MSFTNGP04.phx.gbl...

| because :
|
| for ( int i=0 ; i<maxidx ; ++i )
| for ( int i=0 ; i<maxidx ; i++ )
|
| in both cases the value is used after the operation is applied

But if you do this :

int i = 0;

while (i < maxidx)
Console.WriteLine(i++);

i = 0;

while (i < maxidx)
Console.WriteLine(++i);

You get different results; which is why I mentioned that the two operators
(pre and post) do different things, therefore performance is not an issue in
assessing the two operators. If you want to do two different things, then
you choose the correct operator for the job, performance comparison in this
situation is like comparing apples to bananas.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 21 '06 #7
"Tony Johansson" <jo*****************@telia.com> 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
Tony Johansson <jo*****************@telia.com> 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 - <sk***@pobox.com>
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
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]" <jo****@not.for.spam> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"gerry" <ge**@nospam.nospam> a écrit dans le message de news:
Og**************@TK2MSFTNGP04.phx.gbl...

| because :
|
| for ( int i=0 ; i<maxidx ; ++i )
| for ( int i=0 ; i<maxidx ; i++ )
|
| in both cases the value is used after the operation is applied

But if you do this :

int i = 0;

while (i < maxidx)
Console.WriteLine(i++);

i = 0;

while (i < maxidx)
Console.WriteLine(++i);

You get different results; which is why I mentioned that the two operators
(pre and post) do different things, therefore performance is not an issue in assessing the two operators. If you want to do two different things, then
you choose the correct operator for the job, performance comparison in this situation is like comparing apples to bananas.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

May 21 '06 #10
"gerry" <ge**@nospam.nospam> 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
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote:
"Tony Johansson" <jo*****************@telia.com> 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
"Joanna Carter [TeamB]" <jo****@not.for.spam> 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
"Joanna Carter [TeamB]" <jo****@not.for.spam> 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
Joanna Carter [TeamB] <jo****@not.for.spam> wrote:

<snip>
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 - <sk***@pobox.com>
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> 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
"Barry Kelly" <ba***********@gmail.com> 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
Joanna Carter [TeamB] <jo****@not.for.spam> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> 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 - <sk***@pobox.com>
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> 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
Joanna Carter [TeamB] wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
7
by: BC | last post by:
Hi all, I have a method that accepts a string as the parameter. This can be a base64 image string, or just a normal string. Currently to test whether it's a image string or just a normal string,...
1
by: Stefano Bonnin | last post by:
Only a simple question I have a table with primarykey(field1,field2,field3) and I want to execure a query like select * from my_table where field1 = some_value
28
by: ziman137 | last post by:
Hello all, I have a question and am seeking for some advice. I am currently working to implement an algorithmic library. Because the performance is the most important factor in later...
7
by: Peted | last post by:
Hi, im hoping someone cane provide or point to a definitive accurate explantion of dotnet compilation when run and the best way to optimise peformace when dotnet code is run first time and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.