• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which one is faster ?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be sily question. But doubt came in my mind, so 'm asking.
Which one is more faster! An interface or a class.
Is it a one sided answer, or depends upon the business needs?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a Zen koan. Since an interface has no implementation, can it have speed? When you can answer this question, Grasshopper. . .
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was my first reaction, but actually, there is a way to interpret the question that makes sense: which of the following two method invocations of run() are faster:

Each of these will use a different JVM bytecode to do the lookup, so this is a legitimate question.
Long ago, before many JVMs even had JIT compilers, let alone HotSpot, there was a measurable difference; invocation through an interface was a bit slower in Sun's first JVMs. This make sense if you think about it; whereas "extends polymorphism" can use plain old virtual tables, "interface polymorphism" has to do something a little fancier. Even when there was a difference, though, it wasn't very large.
Nowadays, with HotSpot-type functionality, I'd be surprised if a real-world benchmark could find any measurable difference at all.
Ergo, the decision should alwaysbe based just on design issues, not performance issues. In general, favoring interfaces leads to less coupling and better maintainability down the road.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm afraid that this is a bit like asking "Which is faster, a car door or a car door handle?" Just as it's the engine that moves the car, it's not the interface/class distinction that determines speed; instead, it's the underlying code.
Interfaces would, on average, compile faster, since there's no underlying code to compile, but that's about it. Interfaces are an important part of desing, and you should not shun them because of (unwarrented) fears over speed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic