Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4898726

Nonstatic method calls take lot more time than static method calls

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • hotspot
    • x86
    • linux



      Name: gm110360 Date: 07/30/2003


      A DESCRIPTION OF THE REQUEST :
      I was surprised to see nonstatic method calls taking anyhwere from 25% to 100% more time to execute than static methods. I was hoping it will be in the range of 5%! :-(

      BTW, I got these numbers looking at javacc (http://javacc.dev.java.net) generated code. JavaCC has an option called STATIC which when set, generates static/single use parsers. We introduced this way back in 1996 becase those days nonstatic method calls were extremely expensive - we have seen overheads upto 20 times! Now, I am looking at ways of simplifying and improving JavaCC and wanted to see if STATIC still made sense. I was blown away by the slowdowns with nonstatic methods.

      JUSTIFICATION :
      If fixed. it can enhance performance of almost all Java applications, including appservers etc.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Would like nonstatic methods to be in the 5% overhead range as compared to static methods.
      ACTUAL -
      It is between 25%-100%

      ---------- BEGIN SOURCE ----------
      public class t
      {
         static int cnt;
         static public void f()
         {
            if (cnt-- == 0) return;
            g();
         }

         static public void g() { f(); }

         public void nonstatic_f()
         {
            if (cnt-- == 0) return;
            nonstatic_g();
         }

         public void nonstatic_g() { nonstatic_f(); }

         public static void main(String[] args) throws Throwable
         {
            long l = System.currentTimeMillis();
            int tmp = Integer.parseInt(args[0]);
            try
            {
               t o = new t();
               for (int i = 0; i < tmp; i++) { cnt = tmp; o.f(); }
            }
            finally
            {
               System.out.println("Static calls take: " + (System.currentTimeMillis() - l) + " ms.");
            }

            l = System.currentTimeMillis();
            try
            {
               t o = new t();
               for (int i = 0; i < tmp; i++) { cnt = tmp; o.nonstatic_f(); }
            }
            finally
            {
               System.out.println("Nonstatic calls take: " + (System.currentTimeMillis() - l) + " ms.");
            }
         }
      }



      Just run java t 10000 to see the difference in time taken for static vs. nonstatic
      ---------- END SOURCE ----------
      (Incident Review ID: 192927)
      ======================================================================

            collins Gary Collins (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: