Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ++i is faster than i++ in Java?

Reply
Thread Tools

++i is faster than i++ in Java?

 
 
jrefactors@hotmail.com
Guest
Posts: n/a
 
      10-25-2005
I want to know in Java, is prefix operator faster than postfix
operator?

for example, ++i is faster than i++. I know in C++, this is the case,
but not sure if Java is the same.


please advise. thanks!!

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-25-2005
wrote:

> I want to know in Java, is prefix operator faster than postfix
> operator?


What did your bechmark suggest? Post the code and I'll
give you some figures for my machine as well.
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      10-25-2005
wrote:
> I want to know in Java, is prefix operator faster than postfix
> operator?
>
> for example, ++i is faster than i++. I know in C++, this is the case,
> but not sure if Java is the same.


The issue in C++ occurs when the ++ postfix operator is overloaded. The
overload function needs to return a copy of the original object. The
prefix operator can get away with just returning a reference.

Java does not (yet) allow operator overloading, so the issue does not arise.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-25-2005
On 24 Oct 2005 22:18:33 -0700, wrote, quoted or
indirectly quoted someone who said :

>I want to know in Java, is prefix operator faster than postfix
>operator?
>
>for example, ++i is faster than i++. I know in C++, this is the case,
>but not sure if Java is the same.


Java is a language. It does not have a speed. All you can talk about
is the code generated by some particular compiler/runtime
implementation on some particular computer.

In theory ++i should be slightly slower than i++ since i can be used
right away with i++, then incremented at leisure any time there is a
hole in the instruction pipeline to do it before i is again next
needed. With ++i, all has to wait while i is first incremented.

However, a clever optimiser could likely do either equally quickly by
fitting in the increment ahead of time in some unused pipeline time.

In a machine like the AMDs, you don't even see this level of
optimisation. It is all done on the fly by hardware.

It is not the sort of thing to fret over. Most idiomatic code uses
i++ for historic reasons. Avoiding confusing others by using i++ when
you have a choice.




--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      10-25-2005
Roedy Green wrote:
>
> It is not the sort of thing to fret over. Most idiomatic code uses
> i++ for historic reasons. Avoiding confusing others by using i++ when
> you have a choice.


Or ++i because it is more consistent with C++ code and is a simpler
operator. Just one or the other.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Tris Orendorff
Guest
Posts: n/a
 
      10-25-2005
wrote in
news: ups.com:

> I want to know in Java, is prefix operator faster than postfix
> operator?
>
> for example, ++i is faster than i++. I know in C++, this is the case,
> but not sure if Java is the same.
>
>
> please advise. thanks!!


I advise you to use the ++C compiler because I KNOW it is faster than the
C++ compiler.



--

Sincerely,

Tris Orendorff
[Two antennae meet on a roof, fall in love and get married. The ceremony
wasn't much, but the reception was excellent.]

 
Reply With Quote
 
Kari Ikonen
Guest
Posts: n/a
 
      10-25-2005
wrote:

> I want to know in Java, is prefix operator faster than postfix
> operator?
>
> for example, ++i is faster than i++. I know in C++, this is the case,
> but not sure if Java is the same.
>


Neither one is faster in java, since code generated for
"i++", "++i", "i += 1" and "i = i + 1" is exactly same.

--
KI
 
Reply With Quote
 
Benji
Guest
Posts: n/a
 
      10-25-2005
In comp.lang.java.programmer wrote:
> I want to know in Java, is prefix operator faster than postfix
> operator?


> for example, ++i is faster than i++. I know in C++, this is the case,
> but not sure if Java is the same.


rule 0: the compiler is smarter than you are.

chances are any code that you write with either ++i or i++ is going to
get compiled to the exact same code. if javac (or the java vm, depending
on where the optimization is made) detects that it doesn't to depend on
what the result is, it's probably going to generate the same native code.

Even if there were a slight performance increase, it would be SO
incredibly small compared to all of the other overheads in your program
that it is completely not worth worrying about. I guarentee you that a
there is no program you could write that would result in more than a
..01% overhead due to doing the increment the "wrong way", if there
was a wrong way.

Your real adversary is excessive memory usage on most systems. As far as
how you code your algorithms, what is more important is readability and
maintainability (within reason - obviously if you affect the big-O of
the algorithm, or if it is a very tightly-run loop)

If you have a program where something like that would matter, you
probably shouldn't be using java in the first place, since being
garbage-collected is a pretty big overhead.

But, in summation, I'd be willing to bed that they'd both get compiled
down to the same code. (in java or in c) Especially with variables with
function-scope, the java compiler has plenty of opportunity to optimize
away any "inefficiencies" you might have. You'd be surprised at how
smart they are on the method level.

A common mistake that people make in c is thinking that using inlined
assembly will make their program faster, when in reality, they're just
sticking a block of unoptimizable code in the middle of their function,
and crippling the compiler's ability to do its job as well as it could.

--
Of making better designs there is no end,
and much refactoring wearies the body.
 
Reply With Quote
 
Benji
Guest
Posts: n/a
 
      10-25-2005
In comp.lang.java.programmer wrote:
> I want to know in Java, is prefix operator faster than postfix
> operator?


> for example, ++i is faster than i++. I know in C++, this is the case,
> but not sure if Java is the same.


rule 0: the compiler is smarter than you are.

chances are any code that you write with either ++i or i++ is going to
get compiled to the exact same code. if javac (or the java vm, depending
on where the optimization is made) detects that it doesn't to depend on
what the result is, it's probably going to generate the same native code.

Even if there were a slight performance increase, it would be SO
incredibly small compared to all of the other overheads in your program
that it is completely not worth worrying about. I guarentee you that a
there is no program you could write that would result in more than a
..01% overhead due to doing the increment the "wrong way", if there
was a wrong way.

Your real adversary is excessive memory usage on most systems. As far as
how you code your algorithms, what is more important is readability and
maintainability (within reason - obviously if you affect the big-O of
the algorithm, or if it is a very tightly-run loop)

If you have a program where something like that would matter, you
probably shouldn't be using java in the first place, since being
garbage-collected is a pretty big overhead.

--edit--
actually, if you have a program that is a series of tight loops,
java could actually be faster than c or c++, since it does the
actual compilation to native code on the fly. But anyway, the
point is, you don't really need to worry about it.
--end edit--

But, in summation, I'd be willing to bed that they'd both get compiled
down to the same code. (in java or in c) Especially with variables with
function-scope, the java compiler has plenty of opportunity to optimize
away any "inefficiencies" you might have. You'd be surprised at how
smart they are on the method level.

A common mistake that people make in c is thinking that using inlined
assembly will make their program faster, when in reality, they're just
sticking a block of unoptimizable code in the middle of their function,
and crippling the compiler's ability to do its job as well as it could.

--
Of making better designs there is no end,
and much refactoring wearies the body.
 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      10-25-2005
"Thomas Hawtin" <> wrote in message
news:435e4765$0$49772$...
> Roedy Green wrote:
>>
>> It is not the sort of thing to fret over. Most idiomatic code uses
>> i++ for historic reasons. Avoiding confusing others by using i++ when
>> you have a choice.

>
> Or ++i because it is more consistent with C++ code and is a simpler
> operator. Just one or the other.
>
> Tom Hawtin


C++ uses either. But I have seen i++ (by itself) much more than i++.

I don't know about . . . simpler?

When you are using i in a statement, use the one that is appropriate, they
don't return the same value.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SortedSet faster than Collections.sort() Timo Nentwig Java 6 02-26-2005 01:21 AM
Why .NET is faster than Java Cindi Jenkins Java 1 12-06-2004 03:28 AM
rules engines: faster implementation than methods invocations? NOBODY Java 2 10-30-2004 04:19 AM
Comment : ASP.NET Performs 10 Times Faster than J2EE =?Utf-8?B?Sm9obiBQYXVsLiBBIChNQ1AgSUQjIDMwMTUxNzYp?= ASP .Net 6 07-07-2004 12:46 PM
Anything faster than stat() ? Ken Tucker Perl 1 07-08-2003 06:29 AM



Advertisments