casting affects

casting affects

One of the only websites that I can access from the client site that I'm on is the IBM site, and I must admit that it's not on my list of Java related sites that I regularly visit. So, scanning through some of the forums I came across this thread about the overhead of performing object casting in Java. Interesting, and something that I've never really thought about before. I can certainly see how running all the various checks at runtime can introduce some overhead but I imagine that it's still fairly tiny. I've not read the book that's mentioned (Java Performance Tuning) but perhaps I might look it up now. Hmmm ... interesting.

Cost of a cast I recently wrote a test to measure the time taken to cast an Object to an Integer. The result was about 0.5ns on my 3.06GHz P4. This is very cheap. No doubt some casts will be more expensive, but I would suggest measuring very carefully before placing the blame for performance problems in this area.

casting for comparison There was 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.

casting for comparison Yes, Ken Arnold told us about this at MacHack. He says that with an Exception, hotspot has to de-compile 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. --invalidname

Cost of a cast Hi, Can you tell me how did you meassure the cost of the cast operation? I want to meassure the cost of the instanceOf operation. Thank You very much in advance

Tel: +86-574-86556922