Home arrow static arrow Java Programming [Archive] - Casting costs in JDK 1.4 + Nil?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Casting costs in JDK 1.4 + Nil?
This topic has 21 replies on 2 pages.    1 | 2 | Next »

Posts:11
Registered: 7/17/03
Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 11:33 AM



 
Ok,
I've read the articles on the costs of casting for JDK 1.2.2, etc. on the javaworld site., but was wondering, what's the cost in newer JDK's - so I downloaded the test that the javaworld article used (http://www.sosnoski.com/Performance/Casting.html) and ran it with JDK 1.4.1_02 ---
It looks like the cost of casting is pretty much nill anymore in new JDK's .

Can anyone else collaborate my results?
Also, does anyone know roughly how the performance improvments were made? When I think about casting, it doesn't seem to complicated for the JVM to do - maybe the performance fix is a result of the reflection speedups that have been made, alowing the JVM to type check the cast faster?

Thanks

Mike
 

Posts:27,518
Registered: 11/3/97
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 11:45 AM (reply 1 of 21)



 
I would suppose it is because it compiled the code.
 

Posts:3,258
Registered: 00-08-28
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 11:46 AM (reply 2 of 21)



 
I remember a presentation at JavaOne where someone had run a test with results that suggested it's highly cheaper to test an object using 'instanceof' rather than casting and catching the exception - though in that case I assume most of the overhead is in the ClassCastException.
 

Posts:14,142
Registered: 99-04-02
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 11:46 AM (reply 3 of 21)



 
I would suggest this: If you can avoid casting, then you might as well do so... But there are many places (at least til you move to 1.5 and use autoboxing) where you have little choice but to cast. And even with autoboxing, I'm not sure it'll always eliminate casting. So there are going to be times you have no choice, so whatever performance hit this might cause, you're S.O.L.
 

Posts:11
Registered: 7/17/03
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 11:55 AM (reply 4 of 21)



 
But why avoid casting if there is no performance hit with it?
(Excluding class cast exceptions...)
 

Posts:3,258
Registered: 00-08-28
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 11:58 AM (reply 5 of 21)



 
yes there are places where you cannot avoid it but if I am not wrong Type casts are essentially a call to instanceof, so there is every reason to optimize it in the JVM. A cast like
  ClassX x = (ClassX) y;


is essentially a test like
  if ( (y == null) | | (y instanceof X) )                               assign y to x    else               throw a class cast exception


Correct me if I am wrong.

Primitive casts are different of course. They are a bitmap conversion operation, with no test involved.
 

Posts:3,258
Registered: 00-08-28
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 12:02 PM (reply 6 of 21)



 
yes there are places where you cannot avoid it but if
I am not wrong Type casts are essentially a call to
instanceof, so there is every reason to optimize it in
the JVM. A cast like
ClassX x = (ClassX) y;


is essentially a test like
if ( (y == null) | | (y instanceof X) )assign y to xelsethrow a class cast exception


Correct me if I am wrong.

Primitive casts are different of course. They are a
bitmap conversion operation, with no test involved.

Also I think that this cost would basically be a function of the exception itself rather than anything else because it would involve a whole bunch of code to provide you with a stack trace, which is really expensive if you do it a lot. Granted, getting a stack trace is valuable for debugging problems or noting truly exceptional conditions, but it's a horrid waste of time if what you're really doing is flow control, especially since instanceof gives you a cheap alternative.

 

Posts:14,142
Registered: 99-04-02
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 12:06 PM (reply 7 of 21)



 
But why avoid casting if there is no performance hit
with it?
(Excluding class cast exceptions...)

Well, if there is truly no performance hit, it wounldn't matter. I presume that JIT compilation is what actually removes the performance hit which you'd normally get running without the JIT compilation.
 

Posts:3,258
Registered: 00-08-28
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 12:14 PM (reply 8 of 21)



 
But why avoid casting if there is no performance hit
with it?
(Excluding class cast exceptions...)

Well, if there is truly no performance hit, it
wounldn't matter. I presume that JIT compilation is
what actually removes the performance hit which you'd
normally get running without the JIT compilation.

quite right and for those who would ask whats JIT heres a link

http://wwws.sun.com/software/solaris/jit/
 

Posts:3,258
Registered: 00-08-28
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 12:15 PM (reply 9 of 21)



 
But why avoid casting if there is no performance hit
with it?
(Excluding class cast exceptions...)

Well, if there is truly no performance hit, it
wounldn't matter. I presume that JIT compilation is
what actually removes the performance hit which you'd
normally get running without the JIT compilation.

But just a min. How do you know that the OP is using JIT compiler.
 

Posts:6,750
Registered: 1/25/04
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 12:19 PM (reply 10 of 21)



 
But why avoid casting if there is no performance hit
with it?
(Excluding class cast exceptions...)

Because it reduces compile-time checking.
 

Posts:11
Registered: 7/17/03
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 12:33 PM (reply 11 of 21)



 
True,
But it can make your code more complicated to maintain, read, etc - i.e. collections vs. array....
 

Posts:6,750
Registered: 1/25/04
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 1:12 PM (reply 12 of 21)



 
By "it" I assume you mean "avoiding casting," so then you're saying using arrays (which is one possible way to avoid casting) can make your code harder to read and maintain? I agree.
 

Posts:14,142
Registered: 99-04-02
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 1:17 PM (reply 13 of 21)



 
But just a min. How do you know that the OP is using JIT compiler.

good point..
 

Posts:3,258
Registered: 00-08-28
Re: Casting costs in JDK 1.4 + Nil?  
Aug 4, 2004 1:48 PM (reply 14 of 21)



 
By "it" I assume you mean "avoiding casting," so then
you're saying using arrays (which is one possible way
to avoid casting) can make your code harder to read
and maintain? I agree.

I guess the OP is trying to draw a similarity between using Arrays Vs Collection and

not using casting vs casting.

I however donot see a similarity between the two scenarios but I would however say theres always a cost.

The fastest code is not always the best and neither is the most maintainable one so you have to come to a compromise between the two based on the terms and conditions in any particular case.

e.g

If you are writing a very maintainable code but a slow one for patriot missile You bet theres going to be trouble.
 
This topic has 21 replies on 2 pages.    1 | 2 | Next »