• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance comparison of Java class and Interfaces  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I would like to know if application performance can be improved by replacing a Java class file which contains final static variables with an interface.
Also can someone explain how to measure the time taken by JVM to load a class file against an interface.

Thanks
Joe.
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems VERY unlikely that there would be any difference at all. The distinction between a class and an interface is mainly of interest to the compiler, not to the runtime.
Bill
 
Joe P Kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that mean that loading a class on a JVM and an loading an interface on a JVM will nearly take the same time. I thought an Interface will be light weight for the eyes for compiler as well as interpreter.
Also, can you please explain as to why class / interface are of interest to a compiler and not the Runtime env.

Thanks a lott
Joe.
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain, why you expect a difference.

You could write your own classloader.
This may sound super-complicated, but a lot of people wrote their own classloader, and you only need to find the place, where you modify the classloading - to insert the stopwatch functionality.

I guess the time to load a class or interface will be hard to measure, since it will be a very small time.

Perhaps you may generate a big class and interface with generated code, like

, to get a measurable difference...

Tell us the results.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that mean that loading a class on a JVM and an loading an interface on a JVM will nearly take the same time. I thought an Interface will be light weight for the eyes for compiler as well as interpreter.
Also, can you please explain as to why class / interface are of interest to a compiler and not the Runtime env.

No, it does NOT mean that loading a class will take about the same time - sheesh! Look at the size of the compiled interface and compiled class .class files - unless the interface defines many many constants, it will be quite compact.

As for your second question - think about what the compiler has to do:
1. when a class declaration implements an interface
2. when a reference is an interface type

At runtime, the JVM only needs to know if a reference is the correct type ie is an instance of a class or implementation of an interface. As far as I can see, the checking mechanism is the same for both.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!