• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Paul Clapham
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic