Re: Function call overhead
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <20071207101601234.00000001312@RONYANG01> |
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