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
本文转自:China Industry News