<< Mono's Response To XPF/XAML, Flash and XPF/E | Home | Red Hat Acquires MetaMatrix >>

Brian Coyner: Throwing Null

Brian Coyner: Did you know that you can throw null?

Go read Brian's post now.

Done? Good.

When I ran Brian's example on a three-year-old WinXP Pro SP2 Pentium 4 3.2GHz, 2.5 GB RAM, Java 6, I get the following results:

[weiqi@gao]$ java NullExceptionTest # throwNull 
Average Time (nano): 18022
Average Time (nano): 17965
Average Time (nano): 19184
Average Time (nano): 17377
Average Time (nano): 16760
Average Time (nano): 16423
[weiqi@gao]$ java NullExceptionTest # throwNullPointerException
Average Time (nano): 3884
Average Time (nano): 3327
Average Time (nano): 2980
Average Time (nano): 2669
Average Time (nano): 3024
Average Time (nano): 2724

Throwing null is still more expensive than throwing a new NullPointerException, however the ratio is 5.68 rather than the 45 on Brian's machine.

Now I'm curious how the numbers will respond to arch/os/runtime variations.

[Update] Brian has an update on the numbers with the -server switch turned on:

Brian Coyner: WOW! I was not expecting the tables to be turned.

Tags :


Re: Brian Coyner: Throwing Null

Hi There, Here're my values BOX : AMD Athlon 64 X2 3800+ 2 GHz, 2 GB Ram, WINDOWS XP. java : JDK 1.6.0_01 NullPointerException ==================== Average Time (nano): 4081 Average Time (nano): 3686 Average Time (nano): 3564 Average Time (nano): 3305 Average Time (nano): 3350 Average Time (nano): 3362 throwing null ============= Average Time (nano): 10579 Average Time (nano): 10710 Average Time (nano): 9953 Average Time (nano): 9571 Average Time (nano): 9826 Average Time (nano): 9605 Ratio is 1:3 :-) I guess it keeps getting better!

Re: Brian Coyner: Throwing Null

Putting the results in a nicer format:
NullPointerException
====================
Average Time (nano): 4081
Average Time (nano): 3686
Average Time (nano): 3564
Average Time (nano): 3305
Average Time (nano): 3350
Average Time (nano): 3362

throwing null
=============
Average Time (nano): 10579
Average Time (nano): 10710
Average Time (nano): 9953
Average Time (nano): 9571
Average Time (nano): 9826
Average Time (nano): 9605

Re: Brian Coyner: Throwing Null

That is interesting. I actually think I could get used to "throw null"--it's short and sweet. FWIW, I see the same weird performance differences between OS X and Linux, too.

Re: Brian Coyner: Throwing Null

It get's better still. With Java 4, which is what my out-dated mac mini G4 has, Brian's code doesn't compile. After changing the nanoTime() call to currentTimeMillis(), I get the following compile error:

[weiqi@gao] $ javac NullExceptionTest.java 
NullExceptionTest.java:26: unreported exception <nulltype>; must be caught or declared to be thrown
        throw null;
        ^
1 error
[weiqi@gao] $

I have to change it to

return (NullPointerException) null;

for the class to compile. And here are another set of numbers (Java 1.4.2_12, 1.25 GHz PowerPC G4, 256MB RAM):

[weiqi@gao]$ java NullExceptionTest # NullPointerException 
Average Time (nano): 16
Average Time (nano): 14
Average Time (nano): 12
Average Time (nano): 13
Average Time (nano): 13
Average Time (nano): 11
[weiqi@gao]$ javac NullExceptionTest # throwNull
Average Time (nano): 150
Average Time (nano): 162
Average Time (nano): 168
Average Time (nano): 172
Average Time (nano): 159
Average Time (nano): 168

The ratio is 12.4.


Add a comment Send a TrackBack