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

speed up java.util.Arrays.binarySearch() by 40% by using bit-shift vs. division

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta2
    • x86
    • windows_2000
    • Verified



      Name: krC82822 Date: 07/31/2001


      orig synopsis: "java.util.Arrays.binarySearch() function can easily be sped up by approx 40%"

      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      The binary search java.util.Arrays.binarySearch() is much slower than it needs
      to be. For JDK 1.3.0 on win2000 (and possibly other platforms) using "/2"
      instead of ">>1" is approximately 40% slower.

      All that needs to be done to boost speed is to change the following line from
      this:
      int mid =(low + high)/2;
      to this:
      int mid =(low + high)>>1;

      in the few places it occurs in java.util.Arrays.java

      Better yet, the java compiler should recognise this as a standard optimisation
      and do it whenever possible.
      (Review ID: 129120)
      ======================================================================

            jjb Josh Bloch (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: