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

Function pointer (lambda) invocation in a loop, slows down severely

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      MacOS High Sierra 10.13.3

      A DESCRIPTION OF THE PROBLEM :
      Write a function (say foo) that invokes a function pointer (bar) in a loop for n times. Then invoke the foo() function for several times, and measure the time consumed for each invocation.

      The measured time increases after two rounds of foo() invocation.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Write a function (say foo) that invokes a function pointer (bar) in a loop for n times. Then invoke the foo() function for several times, and measure the time consumed for each invocation.

      The measured time increases after two rounds of foo() invocation.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It is expected to take the same amount of time fo each invocation of foo().
      ACTUAL -
      Start
      Round 1: 5 ms
      Round 2: 29 ms
      Round 3: 3205 ms
      Round 4: 3231 ms
      Round 5: 3185 ms
      Round 6: 3223 ms
      Done

      ---------- BEGIN SOURCE ----------
      public class Sample {

          public static void main(String[] args) {
              System.out.println("Start");
              test(Test4::simpleFunction, "Round 1");
              test(Test4::simpleFunction, "Round 2");
              test(Test4::simpleFunction, "Round 3");
              test(Test4::simpleFunction, "Round 4");
              test(Test4::simpleFunction, "Round 5");
              test(Test4::simpleFunction, "Round 6");
              System.out.println("Done");
          }

          /**
           * Function that invokes a given function pointer in a loop.
           *
           * @param testFunc Function pointer to invoke
           * @param name prefix to print
           */
          private static void test(Function<?, ?> testFunc, String name) {
              long startTime = System.currentTimeMillis();
              for (int i = 0; i < 1_000_000_000; i++) {
                  testFunc.apply(null);
              }
              long endTime = System.currentTimeMillis();
              System.out.println(name + ": " + (endTime - startTime) + " ms");
          }

          private static Object simpleFunction(Object o) {
              return null;
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              vlivanov Vladimir Ivanov
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: