class Foo{
public static void main(String[] args){
long bits = 0L;
long start = System.currentTimeMillis();
int n = 2000000001;
while (n > 0) {
bits ^= (1 << 5);
n--;
}
System.out.println(bits);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
So it isn't a quirk of the bytecode scala produces
On Nov 30, 2007 11:18 PM, Erik Engbrecht <erik.en...@gmail.com> wrote:
If the core 2 is a server-class machine it will run the server VM by
default, which may be able to optimize much of the code away. Do you get
the same results forcing server or client VMs on both machines?
- Charlie
On a 64-bit machine running linux there's no difference between the
client and server times. I don't know whether this is because the
64-bit client and server VM optimisations aren't as different as they
are in 32-bit mode or whether there's some advantage to 64-bit here
(if there is, gcc isn't taking advantage of it, see below).
The obvious C translation of
#include <stdio.h>
int main(){
long bits = 0L;
int n = 2000000001;
while (n > 0) {
bits ^= (1 << 5);
n--;
}
printf("%d", bits);
}
Seems to take longer than the client time, regardless of whether it's
in 64 or 32 bit mode.
class Foo{
public static void main(String[] args){
long bits = 0L;
long start = System.currentTimeMillis();
int n = 2000000001;
while (n > 0) {
bits ^= (1 << 5);
n--;
}
System.out.println(bits);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
Unroll this loop by a factor of 2. So
while (n > 0) {
bits ^= (1 << 5);
n--;
}
becomes
while (n > 1) {
bits ^= (1 << 5);
n--;
bits ^= (1 << 5);
n--;
}
if (n == 1){
bits ^= (1 << 5);
n--;
}
Now we can reorder the unrolled loop as
while (n > 1) {
bits ^= (1 << 5) ^= (1 << 5);
n--;
n--;
}
But
bits ^= (1 << 5) ^= (1 << 5)
is the same as
bits ^= ( (1 << 5) ^ (1 << 5) )
Which is the same as
bits ^= 0
Which is bits = bits, a no-op.
So we can remove that calculation from the loop entirely.
So basically the reason the server VM is so freaking fast is that the
loop body is practically empty. :-) It's just the end result of a few
simple "throwing away uneccessary work" optimisations, nothing magic.
I believe that the 64bit SUN JVM defaults to server mode. The
expectation is that 64bit things would be used on a server.
Richard Warburton
I tried it with the -client flag as well. So it may not be a case of
default so much as "Always runs in server mode".
It would definitely explain some of the problems I've seen with Swing
on 64-bit VMs.
And if I'd spent five minutes googling I would have discovered that
this is indeed the case. Doh.