>>
Krenn I said above....
Whoops, I mean foxyshadis (who quoted Krenn)
HiredGoon
@HiredGoon
Best posts made by HiredGoon
Latest posts made by HiredGoon
-
RE: A Software Engineering Paper WTF
-
RE: A Software Engineering Paper WTF
Krenn I said above that I don't think it is worth it most of the time
(except for my matrix example that avoids a copy-constructor call which
can't be optimized away)
INCONTESTABLE PROOF += is TWICE AS FAST! (disclaimer: Like many benchmarks, may be somewhat contrived)
public class BogusBenchmark {
public static int index(int i) {
try {
Thread.currentThread().sleep(1000);
} catch (Exception e) { }
return i;
}
public static void main(String[] args) {
int[] a = { 0, 0 };
long start = System.currentTimeMillis();
for( int i=0 ; i<10 ; ++i ) {
a[index(i%2)] = a[index(i%2)] + 42;
}
System.out.println("+ -> " + (System.currentTimeMillis() - start));
int[] b = { 0, 0 };
start = System.currentTimeMillis();
for( int i=0 ; i<10 ; ++i ) {
b[index(i%2)] += 42;
}
System.out.println("+= -> " + (System.currentTimeMillis() - start));
}
} -
RE: A Software Engineering Paper WTF
>> Write it, compile it, disassemble it, if your compiler is worth more
than a nickel the assemblys generated will be strictly the same.
I did. It wasn't and I posted it above. Though granted, Javac does cost less than a nickel....
-
RE: A Software Engineering Paper WTF
I'm not arguing that everyone should use += all the time. Premature
optimization and all that. It's like putting a biscuit into your mouth
in 1 go... sure it saves eating time, but it is messy...
The only time I ever conciously use OP= is in tight loops using
overloaded operators in C++ to avoid unneeded copy-constructor calls on
big objects (ie a 4x4 matrix). The point is, the original poster
pointed out a "WTF" thinking there is no speed gain, while there is a
very small one.
This WTF is busted.
-
RE: A Software Engineering Paper WTF
>> Goon, is there even a unit of time that can measure the difference in the two chunks of code you wrote? I'm not sure we can measure fractions of a second that small. But since your brought it up, we'll dive into it.
Yes there is, it's called a load and assignment operation. A fraction of a second that small doesn't matter to us, but it might to a computer that does it a billion times in a loop.
The C++ vector example given has the following overhead: 1 object stack allocation. 1 copy constructor (which in turn calls copy on 3 floats, x,y,z).
>> Any halfway decent compiler will optimize x = x + y to produce the same code as x += y. And if you're not using a halfway decent compiler, you really shouldn't be complaining about your code being slow.
x = x + y evaluates x twice. This cannot always be optimized away as x does not have to be a primitive, perhaps it is inside a collections and requires 2 lookups?
It is not hard to make an example where javac doesn't optimize it away:
int a[] = { 1, 2 };
1. a[0] += a[1];
0: iconst_2
1: newarray int
3: dup
4: iconst_0
5: iconst_1
6: iastore
7: dup
8: iconst_1
9: iconst_2
10: iastore
11: astore_1
12: aload_1
13: iconst_0
14: dup2
15: iaload
16: aload_1
17: iconst_1
18: iaload
19: iadd
20: iastore
21: return
2. a[0] = a[0] + a[1];
0: iconst_2
1: newarray int
3: dup
4: iconst_0
5: iconst_1
6: iastore
7: dup
8: iconst_1
9: iconst_2
10: iastore
11: astore_1
12: aload_1
13: iconst_0
14: aload_1
15: iconst_0
16: iaload
17: aload_1
18: iconst_1
19: iaload
20: iadd
21: iastore
22: return
-
RE: A Software Engineering Paper WTF
They do generate faster code.
They do this because binary operations create temporary objects, while += etc don't.
Ie to demonstrate (primitives etc work the same way) here is a 3 dimensional vector += operation:
vector3
&operator += (const vector3 &rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Now, + has to do exactly the same thing, but it has to create a new
temporary object and return them, so that a = b + c + d + e; will work
and not effect the original value.
- is implemented using += but has the extra temp overhead.
vector3 operator + (const vector3 &lhs, const vector3 &rhs) {
vector3 tmp(a);
tmp += b;
return tmp;
}
- is implemented using += but has the extra temp overhead.