title: Java reflection vs non reflection performance comparison
body: <p>So I tried to test the performance of java reflection. I want to add every value of every field in a class into a map.</p>
  <pre><code> public static class testClass {
      String monday = &quot;Monday&quot;;
      String tuesday = &quot;Tuesday&quot;;
      String wednesday = &quot;Wednesday&quot;;
      String thursday = &quot;Thursday&quot;;
      String friday = &quot;Friday&quot;;
      String saturday = &quot;Saturday&quot;;
      String sunday = &quot;Sunday&quot;;
      ... getter and setters ...
  }
  
  
  public static void main(String args[]) {
      noReflection();
      reflection();
  }
  
  public static void noReflection() {
      testClass test = new testClass();
      long start = System.nanoTime();
      Map myMap = new HashMap&lt;&gt;();
      for (int i = 0; i &lt; RUNS; i++) {
          List&lt;Integer&gt; myList = new ArrayList&lt;&gt;();
          for(myDayz days: myDayz.values()) {
              myList.add(days.ordinal());
          }
          myMap.put(myList.get(0),test.getMonday());
          myMap.put(myList.get(1),test.getFriday());
          myMap.put(myList.get(2),test.getSaturday());
          myMap.put(myList.get(3),test.getSunday());
          myMap.put(myList.get(4),test.getThursday());
          myMap.put(myList.get(5),test.getTuesday());
          myMap.put(myList.get(6),test.getWednesday());
  
          myMap.clear();
      }
      System.out.printf(&quot;no reflection: %,d ns%n&quot;, (System.nanoTime() - start)/RUNS);
  }
  
  public static void reflection() {
      testClass as = new testClass();
      long start = System.nanoTime();
      final String stringClassName = String.class.getName();
  
      Map myMap = new HashMap&lt;&gt;();
      for (int i = 0; i &lt; RUNS; i++) {
          int enumIndex = 0;
          List&lt;Integer&gt; myList = getEnumsAsList(myDayz.class);
          for(Field field : as.getClass().getDeclaredFields()) {
              field.setAccessible(true);
              String fieldTypeName = field.getGenericType().getTypeName();
              try {
                  Object instance = field.get(as);
                  if (instance != null &amp;&amp; fieldTypeName.equals(stringClassName)) {
                      myMap.put(myList.get(enumIndex),(String) instance);
                      enumIndex++;
                  }
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              }
          }
          myMap.clear();
      }
      System.out.printf(&quot;reflection: %,d ns%n&quot;, (System.nanoTime() - start)/RUNS);
  }
  
  private static &lt;E extends Enum&lt;E&gt;&gt; List&lt;Integer&gt; getEnumsAsList(Class&lt;E&gt; eClass) {
      List&lt;Integer&gt; enumOrdinals = new ArrayList&lt;&gt;();
      for (E en : EnumSet.allOf(eClass))
      {
          enumOrdinals.add(en.ordinal());
      }
      return enumOrdinals;
  }
  
  private enum myDayz {
      MONDAY,
      TUESDAY,
      WEDNESDAY,
      THURSDAY,
      FRIDAY,
      SATURDAY,
      SUNDAY
  }
  </code></pre>
  <p>Now what suprised me is that the non reflection code is about ~60% faster. I thought it would be ALOT faster. My question is if this is what you can expect or have i done something wrong here?</p>