Re: Function call overhead

Ted Neward <[email protected]> Sat, 8 Dec 2007 16:38:38 -0800
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <031a01c839fb$db6b3570$9241a050$@net>
That shouldn't be surprising--volatile accesses are going to be more
expensive than non-volatile ones, since the VM can cache non-volatile values
outside of the main heap.

But never forget, microbenchmarking is a dangerous thing.

Ted Neward
Java, .NET, XML Services
Consulting, Teaching, Speaking, Writing
http://www.tedneward.com
 

> -----Original Message-----
> From: Discussion of advanced Java topics. [mailto:ADVANCED-
> [email protected]] On Behalf Of [email protected]
> Sent: Friday, December 07, 2007 10:16 AM
> To: [email protected]
> Subject: Re: [ADVANCED-JAVA] Function call overhead
> 
> I can't get a difference as large as yours, my times converge to 156ms
> versus 203ms.  My guess is that the cost comes from changing pos to a
> field.  I guess it's no big surprise that field writes are slower than
> local variable writes.  However I was surprised by the cost of changing
> pos to volatile: the time exploded to 4.5sec.
> 
> import java.util.Random;
> public class F {
>   public static void main(String[] args) throws Throwable {
>     for (int x = 0; x < 100; x++)
>       main2(args);
>   }
>   public static void main2(String[] args) throws Throwable {
>     Random random = new Random();
>     byte[] bytes = new byte[64 * 1024 * 1024];
>     random.nextBytes(bytes);
>     int pos = 0;
>     int count = bytes.length;
>     long start2 = System.currentTimeMillis();
>     while (true) {
>       int value = (pos < count) ? (bytes[pos] & 0xff) : -1;
>       ++pos;
>       if (value == -1)
>         break;
>     }
>     System.out.println("TIME TAKEN A : " + (System.currentTimeMillis()
> - start2));
>     final MyClass myClass = new MyClass(bytes);
>     long start = System.currentTimeMillis();
>     while (true) {
>       int read = myClass.read();
>       if (read == -1)
>         break;
>     }
>     System.out.println("TIME TAKEN B : " + (System.currentTimeMillis()
> - start));
>   }
> }
> class MyClass {
>   private final byte[] buf;
>   private /* volatile */ int pos;
>   private int count;
>   public MyClass(byte[] buf) {
>     this.buf = buf;
>     this.count = buf.length;
>   }
>   public final int read() {
>     if (pos < count) {
>       return (buf[pos++] & 0xFF);
>     } else
>       return -1;
>   }
> }
> 
> > -----Original Message-----
> > From: Discussion of advanced Java topics.
> > [mailto:[email protected]] On Behalf Of
> > Avinash Lakshman
> > Sent: Friday, December 07, 2007 12:05 AM
> > To: [email protected]
> > Subject: [ADVANCED-JAVA] Function call overhead
> >
> > I am seeing this wierd problem. When I do the following I see
> > the time taken is around 170 ms.
> >
> > public static void main(String[] args) throws Throwable
> >     {
> >         Random random = new Random();
> >         byte[] bytes = new byte[64*1024*1024];
> >         random.nextBytes(bytes);
> >
> >         int pos = 0;
> >         int count = bytes.length;
> >         long start2 = System.currentTimeMillis();
> >         while ( true )
> >         {
> >             int value = (pos < count) ? (bytes[pos] & 0xff) : -1;
> >             ++pos;
> >             if ( value == -1 )
> >                 break;
> >         }
> >         System.out.println("TIME TAKEN : " +
> > (System.currentTimeMillis() - start2)); }
> >
> > Now I do the following I see the time taken is 516 ms:
> >
> > class MyClass
> > {
> >     private byte[] buf = new byte[0];
> >     private int pos;
> >     private int count;
> >
> >     public final int read()
> >         {
> >             if (pos < count)
> >             {
> >                 return ( buf[pos++] & 0xFF );
> >             }
> >             else
> >                 return -1;
> >         }
> > }
> >
> > public void main(String[] args)
> > {
> >      MyClass myClass = new MyClass(bytes);
> >         int read = 0;
> >         long start = System.currentTimeMillis();
> >         while ( true )
> >         {
> >             read = myClass.read();
> >             if ( read == -1 )
> >                 break;
> >         }
> >         System.out.println("TIME TAKEN : " +
> > (System.currentTimeMillis() - start)); }
> >
> > What is going on?
> >
> > Thanks
> > A
> >
> > _________________________________________________________________
> > Your smile counts. The more smiles you share, the more we
> > donate.  Join in.
> > www.windowslive.com/smile?ocid=TXT_TAGLM_Wave2_oprsmilewlhmtagline
> > ===================================
> > This list is hosted by DevelopMentor®  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
> >
> 
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
> 
> View archives and manage your subscription(s) at
> http://discuss.develop.com
> 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.503 / Virus Database: 269.16.17/1177 - Release Date:
> 12/7/2007 1:11 PM
> 

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.503 / Virus Database: 269.16.17/1178 - Release Date: 12/8/2007
11:59 AM
 

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com