Frugal Server Code Pays Off

Charles Forsythe <[email protected]> Tue, 12 Oct 2004 18:14:43 -0500
Newsgroups gmane.comp.java.advanced-servlets
Message-ID <[email protected]>
Nic Ferrier wrote:

>*Go and test it* on different VMs
>  
>
OK, fine.  I wrote two classes, Foo and Bar that implement equals().  
Both fulfill the requirement that equals() return false if passed null, 
but Foo uses an Exception to detect this, while Bar tests for it using 
equality (See code below).  After called equals(null) 10 times, the 
execution time was computed.  Here are the averages I measured:

    Testing Bar.equals(null)
        Average time: 4ms

    Testing Foo.equals(null)
        Average time: 9ms

This shows that the Foo (Exception-based) equals() took more than twice 
the time as the equals() in Bar.  Note that the stack was only 2 or 3 
frames deep.  It would be interesting to artificially deepen the stack, 
but I think this simple test proved the point: Exceptions are not free.  
The conceit that any single server-based operation requires essentially 
zero time is why J2EE is such a bloated mess. 4ms is an ETERNITY by most 
"server" standards and yet, that's the time is takes MY server to do 
something easy. 

Here are the details about what I ran the test on:

    VM: TINI VM 1.14
        http://www.ibutton.com/TINI/index.html

    HW: DS80C400 @ 29Mhz on a Systronix TStik
        http://www.tstik.com/

As it is, embedded Servlets are churning along pretty well.  My big 
"Hey! What about embedded Servers" flame is going to be targeted at the 
TLS Working Group when I get some data collected...

Here's my Test Code:
--------------8<------------------------------------
public class Xtest
{
    public static final int SAMPLES = 10;

    public static void main(String[] args)
    {
        long[] tstart = new long[SAMPLES];
        long[] tend = new long[SAMPLES];

        System.out.println("Testing Bar.equals(null)");
        Bar bar = new Bar();
        for(int i = 0; i < SAMPLES; i++) {
            tstart[i] = System.currentTimeMillis();
            bar.equals(null);
            tend[i] = System.currentTimeMillis();
        }

        long tavg = 0, etime = 0;

        tavg = 0;
        for(int i = 0; i < SAMPLES; i++) {
            etime = tend[i] - tstart[i];
            tavg += etime;
            System.out.println("Trial #" + i + " took " + etime + "ms");
        }
        System.out.println("Average time: " + (tavg / 10) + "ms");
       
        System.out.println("\r\n\r\n");

        System.out.println("Testing Foo.equals(null)");
        Foo foo = new Foo();
        for(int i = 0; i < SAMPLES; i++) {
            tstart[i] = System.currentTimeMillis();
            foo.equals(null);
            tend[i] = System.currentTimeMillis();
        }

        tavg = 0;
        for(int i = 0; i < SAMPLES; i++) {
            etime = tend[i] - tstart[i];
            tavg += etime;
            System.out.println("Trial #" + i + " took " + etime + "ms");
        }
        System.out.println("Average time: " + (tavg / 10) + "ms");
    }

    static class Foo
    {
        public int baz = 0;

        public boolean equals(Object o)
        {
            try {
                Foo that = (Foo)o;
                return this.baz == that.baz;
            } catch(NullPointerException npx) {
                return false;
            }
        }
    }

    static class Bar
    {
        public int baz = 0;

        public boolean equals(Object o)
        {
            if (o == null) return false;
            Bar that = (Bar)o;
            return this.baz == that.baz;
        }
    }
}
-------------->8-------------------------------------




------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

Before posting a question, try to find your answer here: 
<http://www.egroups.com/links/advanced-servlets>
Announcements should go to: [email protected]
To Post a message, send it to: [email protected]
To Unsubscribe, send a blank message to: [email protected] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/advanced-servlets/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/