Function call overhead

Avinash Lakshman <[email protected]> Fri, 7 Dec 2007 02:05:27 -0600
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <[email protected]>
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