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

Enum.hashCode() could be seven times faster

XMLWordPrintable

    • b71
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      Enum.hashCode() call System.identityHashCode() that
      is seven times slower than Object.hashCode().

      Note that I don't know why and perhaps, System.identityHashCode()
      may be optimized.

      JUSTIFICATION :
      it's improve performance

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      public final int hashCode() {
          return super.hashCode();
      }
      ACTUAL -
      public final int hashCode() {
          return System.identityHashCode(this);
      }

      ---------- BEGIN SOURCE ----------
      It's just a micro benchmark than test super.hashCode()
      and System.identityHashCode(this)

      /**
       * @author remi
       *
       */
      public class MyEnumTest {
        static class Zuper {
          public final int hashCode() {
            return super.hashCode();
          }
        }
        static class Identity {
          public final int hashCode() {
            return System.identityHashCode(this);
          }
        }
        static long test(Object o) {
          long time=System.nanoTime();
          for(int i=0;i<1000000;i++)
            o.hashCode();
          return System.nanoTime()-time;
        }
        public static void main(String[] args) {
          System.out.println("super "+test(new Zuper()));
          System.out.println("identity "+test(new Identity()));
        }
      }

      ---------- END SOURCE ----------
      ###@###.### 10/21/04 23:26 GMT

            ahe Peter Ahe
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: