Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
A HotSpot oddity
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
David Pollak  
View profile  
 More options Nov 30 2007, 5:49 pm
From: "David Pollak" <feeder.of.the.be...@gmail.com>
Date: Fri, 30 Nov 2007 14:49:47 -0800
Local: Fri, Nov 30 2007 5:49 pm
Subject: A HotSpot oddity

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

  wierd.zip
2K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Engbrecht  
View profile  
 More options Nov 30 2007, 6:18 pm
From: "Erik Engbrecht" <erik.engbre...@gmail.com>
Date: Fri, 30 Nov 2007 18:18:26 -0500
Local: Fri, Nov 30 2007 6:18 pm
Subject: Re: A HotSpot oddity

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 <feeder.of.the.be...@gmail.com> wrote:

--
http://erikengbrecht.blogspot.com/

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David MacIver  
View profile  
 More options Nov 30 2007, 8:12 pm
From: "David MacIver" <david.maci...@gmail.com>
Date: Sat, 1 Dec 2007 01:12:10 +0000
Local: Fri, Nov 30 2007 8:12 pm
Subject: Re: A HotSpot oddity
On Nov 30, 2007 11:18 PM, Erik Engbrecht <erik.engbre...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David MacIver  
View profile  
 More options Nov 30 2007, 8:13 pm
From: "David MacIver" <david.maci...@gmail.com>
Date: Sat, 1 Dec 2007 01:13:27 +0000
Local: Fri, Nov 30 2007 8:13 pm
Subject: Re: A HotSpot oddity
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 <erik.engbre...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Pollak  
View profile  
 More options Nov 30 2007, 10:18 pm
From: "David Pollak" <feeder.of.the.be...@gmail.com>
Date: Fri, 30 Nov 2007 19:18:50 -0800
Local: Fri, Nov 30 2007 10:18 pm
Subject: Re: A HotSpot oddity

Yeah, the Scala code was a port of Java code that does similar things.  With
the Scala command line and some other Scala tools, it's much faster to make
a change and test the change with Scala than with Java.

On 11/30/07, David MacIver <david.maci...@gmail.com> wrote:

--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Charles Oliver Nutter  
View profile  
 More options Dec 1 2007, 12:01 am
From: Charles Oliver Nutter <charles.nut...@sun.com>
Date: Fri, 30 Nov 2007 23:01:06 -0600
Local: Sat, Dec 1 2007 12:01 am
Subject: Re: A HotSpot oddity

David Pollak wrote:
> 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?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David MacIver  
View profile  
 More options Dec 1 2007, 5:53 am
From: "David MacIver" <david.maci...@gmail.com>
Date: Sat, 1 Dec 2007 10:53:15 +0000
Local: Sat, Dec 1 2007 5:53 am
Subject: Re: A HotSpot oddity
More interesting data points:

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.

On Dec 1, 2007 3:18 AM, David Pollak <feeder.of.the.be...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Neil Bartlett  
View profile  
 More options Dec 1 2007, 6:02 am
From: Neil Bartlett <njbartl...@gmail.com>
Date: Sat, 1 Dec 2007 11:02:27 +0000
Local: Sat, Dec 1 2007 6:02 am
Subject: Re: A HotSpot oddity
"Scala faster than C" post to reddit, anyone? ;-)

On 1 Dec 2007, at 10:53, David MacIver wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David MacIver  
View profile  
 More options Dec 1 2007, 6:24 am
From: "David MacIver" <david.maci...@gmail.com>
Date: Sat, 1 Dec 2007 11:24:56 +0000
Local: Sat, Dec 1 2007 6:24 am
Subject: Re: A HotSpot oddity
Surely it's no news that Java is competitive with C for pure numeric
operations.

On Dec 1, 2007 11:02 AM, Neil Bartlett <njbartl...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David MacIver  
View profile  
 More options Dec 1 2007, 7:25 am
From: "David MacIver" <david.maci...@gmail.com>
Date: Sat, 1 Dec 2007 12:25:22 +0000
Local: Sat, Dec 1 2007 7:25 am
Subject: Re: A HotSpot oddity
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 <david.maci...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Warburton  
View profile  
 More options Dec 1 2007, 11:17 am
From: "Richard Warburton" <richard.warbur...@gmail.com>
Date: Sat, 1 Dec 2007 16:17:13 +0000
Local: Sat, Dec 1 2007 11:17 am
Subject: Re: A HotSpot oddity

> 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).

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David MacIver  
View profile  
 More options Dec 1 2007, 11:31 am
From: "David MacIver" <david.maci...@gmail.com>
Date: Sat, 1 Dec 2007 16:31:31 +0000
Local: Sat, Dec 1 2007 11:31 am
Subject: Re: A HotSpot oddity
On Dec 1, 2007 4:17 PM, Richard Warburton <richard.warbur...@gmail.com> wrote:

> > 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).

> I believe that the 64bit SUN JVM defaults to server mode.  The
> expectation is that 64bit things would be used on a server.

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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David MacIver  
View profile  
 More options Dec 1 2007, 11:33 am
From: "David MacIver" <david.maci...@gmail.com>
Date: Sat, 1 Dec 2007 16:33:35 +0000
Local: Sat, Dec 1 2007 11:33 am
Subject: Re: A HotSpot oddity
On Dec 1, 2007 4:31 PM, David MacIver <david.maci...@gmail.com> wrote:

And if I'd spent five minutes googling I would have discovered that
this is indeed the case. Doh.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google