I think I've figured out what's going on. I can't figure out how to
get sufficient debug information out of the server hotspot to verify
this, but it seems to make sense and performing the translation
manually produces comparable speedups in the client VM.
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.
On Dec 1, 2007 1:13 AM, David MacIver <[EMAIL PROTECTED]> wrote:
> For what it's worth, the obvious Java translation does the same thing:
>
> 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 <[EMAIL PROTECTED]> wrote:
>
> > David,
> > For me 1.5 and 1.6 perform about the same...1.6 is a little faster.
> > However, -server yields the surprisingly fast results that you are seeing
> > under 1.6 and -client yields the slow results that you are seeing under 1.5.
> >
> > It still doesn't explain why it's so fast...but it's another data point.
> > I'm using an ancient computer with Linux.
> >
> > -Erik
> >
> >
> > On Nov 30, 2007 5:49 PM, David Pollak < [EMAIL PROTECTED]>
> > wrote:
> > > I've got the following Scala code:
> > > object Foo {
> > > def main(argv: Array[String]) {
> > > var bits = 0L
> > > val start = System.currentTimeMillis()
> > > var n = 2000000001
> > > // var n = 2000000001L // makes things very slow
> > > while (n > 0) {
> > > bits = bits ^ (1 << 5)
> > > n = n - 1
> > > }
> > > System.out.println(bits)
> > > val end = System.currentTimeMillis()
> > > System.out.println(end-start)
> > > }
> > > }
> > >
> > > I'm enclosing the source and the bytecode.
> > >
> > > There are 2B iterations.
> > >
> > > On my Core 2 Quad running JDK 1.6 (32 bit), the code takes 2 ms to run.
> > >
> > > On my Mac Book Pro (Core Duo, JDK 1.5) it takes 6,600 ms.
> > >
> > > The run time on the Mac seems more "reasonable". What's going on?
> > >
> > >
> > >
> > > --
> > > lift, the secure, simple, powerful web framework http://liftweb.net
> > > Collaborative Task Management http://much4.us
> > >
> > >
> >
> >
> >
> > --
> > http://erikengbrecht.blogspot.com/
> >
> >
> > > >
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM
Languages" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---