473,322 Members | 1,405 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,322 software developers and data experts.

Any difference???

Is there any difference between i++ & i=i+1...

I mean, I read somewhere that i++ is faster then i=i+1..
But as both of them is performing same operations....how can one be
faster than another...

Jun 11 '07 #1
7 1353
On 11 Juni, 14:28, anuradhathakur...@gmail.com wrote:
Is there any difference between i++ & i=i+1...

I mean, I read somewhere that i++ is faster then i=i+1..
But as both of them is performing same operations....how can one be
faster than another...
In effect there's no difference, and on a good compiler there should
be none in efficiency either. However I think you confused it with the
question of ++i vs i++, where there's a difference. In the case of i++
the value returned is i before the increment whereas ++i will return
the value of i *after* the increment. This means that i++ must make a
copy of the old value before incrementing and then return the copy.
Most compilers can probably optimize away the difference if the result
is not assigned, at least for built-in types.

When using iterators on the other hand there can be a more noticable
difference since an iterator is larger so more have to be copied, for
that reason ++i is generally preferred when just incrementing.

--
Erik Wikström

Jun 11 '07 #2
Erik Wikström wrote:
On 11 Juni, 14:28, anuradhathakur...@gmail.com wrote:
>Is there any difference between i++ & i=i+1...

I mean, I read somewhere that i++ is faster then i=i+1..
But as both of them is performing same operations....how can one be
faster than another...

In effect there's no difference, and on a good compiler there should
be none in efficiency either.
Well, there is, indeed. when i = 1, i++ will return 1 and i=i+1 will
return 2. So, actually the second form can be implemented more efficiently.

Regards,

Zeppe
Jun 11 '07 #3
On 6 11 , 8 28 , anuradhathakur...@gmail.com wrote:
Is there any difference between i++ & i=i+1...

I mean, I read somewhere that i++ is faster then i=i+1..
But as both of them is performing same operations....how can one be
faster than another...
on the assamble side:

the compiler could translate the "i++" to asm: "INC reg"
then the i=i+1 could be "ADD reg,1"

maybe the "INC" instruction runs faster than the "ADD" .
But those difference depends on the optimization method of the
compiler.

Jun 11 '07 #4
Asm23 wrote:
On 6 11 , 8 28 , anuradhathakur...@gmail.com wrote:
>Is there any difference between i++ & i=i+1...

I mean, I read somewhere that i++ is faster then i=i+1..
But as both of them is performing same operations....how can one be
faster than another...

on the assamble side:

the compiler could translate the "i++" to asm: "INC reg"
then the i=i+1 could be "ADD reg,1"
Could. Or they both could result in the same INC instruction. The
compilers of today are relatively clever to recognise 'i=i+1' as
a specific pattern.
maybe the "INC" instruction runs faster than the "ADD" .
But those difference depends on the optimization method of the
compiler.
The question is, would such a difference be noticeable in the big
picture? And, of course, there is the second question, whether the
operator+(int) and operator++(int) are overloaded for the type of
the 'i' object and how well.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 11 '07 #5
I may be able to tell you in different point of view.
These days, compliers are getting better. so when you write i=i+1,
they recognize and change to i++;
so the problem is what compiler you will use.

Anyway, if the compiler is bad enough not to change i=i+1 to i++ and
cpu was old, you can see different machine instructions.
machine instructions depend on the cpu.
However, I think these days that is not a thing concerned.
That's like memory leak in C++ , which you don't have to concern in
Java.
Happy programming

Regards,

Namh

Jun 11 '07 #6
ga******@gmail.com wrote:
I may be able to tell you in different point of view.
These days, compliers are getting better. so when you write i=i+1,
they recognize and change to i++;
It should actually change it to ++i.
so the problem is what compiler you will use.

Anyway, if the compiler is bad enough not to change i=i+1 to i++ and
cpu was old, you can see different machine instructions.
machine instructions depend on the cpu.
However, I think these days that is not a thing concerned.
It's not really the matter of "these days" or "some other days". It's
the matter of how big that difference is relative to the rest of the
program's execution time.
That's like memory leak in C++ , which you don't have to concern in
Java.
Why in hell did you have to bring in Java?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 11 '07 #7

Why in hell did you have to bring in Java?
just i wanna say you don't have to concern about which will use among i
++ and i=i+1 if the compiler can optimize your source code.
Jun 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
21
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
21
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
3
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
9
by: viki1967 | last post by:
Hi all! This new forum its great! :) Congratulations !!! My answer: why this my code not working? Nothing error but not work the difference.... : <html>
11
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.