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

Using Console.println is unnecessarily slow due to JLine initalization

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 23
    • 23
    • tools
    • None
    • b26

      Consider code like:
      ```
      public class ConsoleTest {
          public static void main(String... args) {
              if (args.length > 0) {
                  System.out.println("Hello, World!");
              } else {
                  System.console().printf("Hello, World!\n");
              }
          }
      }
      ```

      There is a significant difference in running time between the two variants, producing the following typical outputs:
      ---
      # for console variant:
      $ time java -classpath /tmp/ ConsoleTest
      Hello, World!

      real 0m0,169s
      user 0m0,371s
      sys 0m0,025s

      # for System.out variants:
      $ time ./build/linux-x86_64-server-release/images/jdk/bin/java -classpath /tmp/ ConsoleTest old
      Hello, World!

      real 0m0,033s
      user 0m0,013s
      sys 0m0,018s
      ---

      Most of the difference appears to be a JLine initialization.

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: