aspose file tools*
The moose likes Performance and the fly likes can the use of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "can the use of "instanceof" operator make the code expensive (time consuming / low performing ) ?" Watch "can the use of "instanceof" operator make the code expensive (time consuming / low performing ) ?" New topic
Author

can the use of "instanceof" operator make the code expensive (time consuming / low performing ) ?

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

just to make it to the point -

I'm having thousands of files coming in each hour, and I have to parse it.. and do some usual stuff.
Now the problem is, to carry out some logic I am putting on some checks using "instanceof" operator in my code, now the architect says "we should be using a boolean under some conditions... its not wise to use "instanceof" as they will make the application expensive...

Is it so ? should I go with the boolean approach... if this is the case... why does java even has that operator..... I love java... I want to use everything..
" My heart says yes... my architect says no " - well give me some reason which he wont give...


Time is what we want the most, but what we use the worst. -- William Penn
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Expensive compared to what?

If you have two alternatives, it's possible to compare them. If you have only one, there's no point asking the question. This "using a boolean" idea would need to have a concrete implementation before you could consider it, otherwise it's just hand-waving.

But before even asking the question, you should profile the application and see if the line of code containing "instanceof" is using a significant percentage of CPU or elapsed time or whatever you're concerned with. I'm willing to bet that it doesn't, and that your architect is prematurely optimizing.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Yay!.. thanks.. and yes "expensive" was referring to performance (CPU time) - as there are I/O operations involved and for a very huge number of files...anyways.. I'll extend your bet! I will try to prove my point again to him..) till I come running to coderanch .. will keep you posted
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Your compiled Java code has loads of instanceof operations, even though you didnt put them in. EVERY reference assignment that is at all in doubt is checked. Furthermore, the designers have put a big effort into making instanceof very efficient.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can the use of "instanceof" operator make the code expensive (time consuming / low performing ) ?