From: Lew on
Arne Vajhøj wrote:
>>>> BTW, isn't ++something a C++'ism?

Lew wrote:
>>> It's been around longer than that, since C.

Arne Vajhøj wrote:
>> I know, but the ++something is better than something++
>> because it is faster is rooted in C++ classes I believe.

I wouldn't know. This is a Java newsgroup. I'm not aware of any speed
differences between the two expressions in Java, and would be dubious of any
claims that there are.

Eric Sosman wrote:
> When I actually want the value of the expression, I write
> whichever I need (usually a[x++] or a[--x]). When all I want
> is the side-effect, I write ++x because "increment x" seems to
> read more smoothly than "x increment."

Which effect is the "side" effect? Isn't incrementation a primary effect of
the "increment" operator?

I use ++x where I want the value of the expression to be the incremented
value, even if I'm discarding that value. I use x++ where I want the value of
the expression to be the not-yet-incremented value, which would be pointless
to throw away.

Plus what Eric said.

--
Lew
From: Patricia Shanahan on
Mike Schilling wrote:
> Arne Vajh�j wrote:
>> On 15-02-2010 17:17, Lew wrote:
>>> Arne Vajh�j wrote:
>>>> BTW, isn't ++something a C++'ism?
>>> It's been around longer than that, since C.
>> I know, but the ++something is better than something++
>> because it is faster is rooted in C++ classes I believe.
>
> Right, in classes which overload '++".
>
> y = ++x + z;
>
> simply calls the ++ method and add z to the result, while
>
> y = x++ + z;
>
> needs to make a copy of x, add z to it, assign the result to y, and then
> call the ++ method on the "real" x. Depending on how complicated "x" is,
> the copy may be a significant expense.

This effect happens, to some degree, even for simple integers in C. I
remember having to allocate an extra register in some situations for
x++. The code for ++x could use the same register to represent x and to
carry the intermediate result.

Patricia
From: Mike Schilling on
Patricia Shanahan wrote:
> Mike Schilling wrote:
>> Arne Vajh�j wrote:
>>> On 15-02-2010 17:17, Lew wrote:
>>>> Arne Vajh�j wrote:
>>>>> BTW, isn't ++something a C++'ism?
>>>> It's been around longer than that, since C.
>>> I know, but the ++something is better than something++
>>> because it is faster is rooted in C++ classes I believe.
>>
>> Right, in classes which overload '++".
>>
>> y = ++x + z;
>>
>> simply calls the ++ method and add z to the result, while
>>
>> y = x++ + z;
>>
>> needs to make a copy of x, add z to it, assign the result to y, and
>> then call the ++ method on the "real" x. Depending on how
>> complicated "x" is, the copy may be a significant expense.
>
> This effect happens, to some degree, even for simple integers in C. I
> remember having to allocate an extra register in some situations for
> x++. The code for ++x could use the same register to represent x and
> to carry the intermediate result.

True, though it's worse for overloaded operations for at least two reasons:

1. The expense of generating an extra integer (allocating an extra register
or performing an extra store and fetch) is fixed, while the expense of
creating a temporary object is unbounded.

2. Since the code generator understands integer arithmetic, it can often
avoid any extra expense at all. The above, for example, can simply be
generated as

inc x
mov x, y
add z, y

vs.

mov x, y
add z, y
inc x

This kind of optimization isn't possible when all that's known about + and
++ is that they're method calls..


From: Arne Vajhøj on
On 15-02-2010 18:47, Lew wrote:
> Arne Vajhøj wrote:
>>>>> BTW, isn't ++something a C++'ism?
>
> Lew wrote:
>>>> It's been around longer than that, since C.
>
> Arne Vajhøj wrote:
>>> I know, but the ++something is better than something++
>>> because it is faster is rooted in C++ classes I believe.
>
> I wouldn't know. This is a Java newsgroup. I'm not aware of any speed
> differences between the two expressions in Java, and would be dubious of
> any claims that there are.

Neither am I.

But I think the something++ notation is more readable and
the main reason why the ++something is used is the influence
of C++ programmer micro optimization.

Arne
From: Arne Vajhøj on
On 15-02-2010 18:18, Eric Sosman wrote:
> On 2/15/2010 5:48 PM, Arne Vajhøj wrote:
>> On 15-02-2010 17:17, Lew wrote:
>>> Arne Vajhøj wrote:
>>>> BTW, isn't ++something a C++'ism?
>>>
>>> It's been around longer than that, since C.
>>
>> I know, but the ++something is better than something++
>> because it is faster is rooted in C++ classes I believe.
>
> When I actually want the value of the expression, I write
> whichever I need (usually a[x++] or a[--x]). When all I want
> is the side-effect, I write ++x because "increment x" seems to
> read more smoothly than "x increment."

If there is a functional difference, then what to
use is given.

I agree with your readability comment, but it does
not convince me because "incremented x" is less
readable than "x incremented".

Arne