Function Call Overhead in Instance Methods

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Function Call Overhead in Instance Methods

My company uses a Java application for processing events in logs, including a scripting interface.  An implemented script simply defines a set of functions which are called by the Java application.  I have added several classes within a script to improve readability and modularity, but I've found that calls to outside functions from within an instance method of a class execute significantly slower than the same functions called outside of a class.

A simplified example:

public PrintStream output = new PrintStream(...);

public class aClass {
  public doStuff() {
    for (int i = 0; i < 100; ++i)
      output.print(" ");

public aClass theClass = new aClass();

public void doStuff() {
  for (int i = 0; i < 100; ++i)
    output.print(" ");

Calling aClass.doStuff() seems to take roughly 10-20 times longer than a call to doStuff(), despite the two executing identical code!  I have tried various combinations of global vs. instance variables, as well as moving the bottleneck calls to a global function in the same script but outside the class, all with no apparent effect.  Even a call to a completely empty function in the same script exhibits the same slowness.

Could there be something wrong with the namespace of the classes I've defined that would cause all this extra overhead?  Any help would be greatly appreciated!