468,483 Members | 2,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,483 developers. It's quick & easy.

Java and the increment operator

I know that in many C/C++ compilers using i++ is faster/ more efficient than
i = i+1.
Is it true for Java as well?

Tony
Jul 17 '05 #1
2 7489
Tony Burrows wrote:
I know that in many C/C++ compilers using i++ is faster/ more efficient than
i = i+1.
Is it true for Java as well?


Well, I don't know, but I would think that it depends on the JVM and
compiler implementation, and of course the architechture it's run on, as
it is in C/C++

The main reason for the i++ being faster than an i=i+1 is that an
increment-instruction (if supported by the hardware) is a often a lot
faster than an addition-instruction. This is not always true - on many
RISC systems the time spent on these will be the same (one clock cycle).
But, all that being said, the compiler really should optimize the i=i+1
to be i++ anyways...

--
Michael Banzon
http://michael.banzon.dk/
Jul 17 '05 #2
Using i++ in C++ is mostly less efficient than using ++i or i += 1. The
problem is that i++ is supposed to yield the old value of i, which means it
is roughly equivalent to (and usually implemented as)

(temp = i, ++i, temp)

Many compilers will detect the non-use of temp so they may optimize away the
assignment. Java compiles to quite understandable bytecode. Why don't you
check the compilers output?

Regards,

Silvio Bierman

"Tony Burrows" <to**@tonyburrows.com> wrote in message
news:d1*******************@news.demon.co.uk...
I know that in many C/C++ compilers using i++ is faster/ more efficient than i = i+1.
Is it true for Java as well?

Tony

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Robert Swan | last post: by
13 posts views Thread by kailasam | last post: by
458 posts views Thread by wellstone9912 | last post: by
11 posts views Thread by divya_rathore_ | last post: by
13 posts views Thread by jehugaleahsa | last post: by
350 posts views Thread by Lloyd Bonafide | last post: by
13 posts views Thread by eternalLearner | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.