Demonstration of speed of reflexive versus programmatic invocation : Class « Reflection « Java






Demonstration of speed of reflexive versus programmatic invocation

         
 
 
 
 

/*
 *     file: ReflexiveInvocation.java
 *  package: oreilly.hcj.reflection
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
 ########## DO NOT EDIT ABOVE THIS LINE ########## */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Demonstration of speed of reflexive versus programatic invocation.
 * 
 * @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
 * @version $Revision: 1.3 $
 */
public class ReflexiveInvocation {
  /** Holds value of property value. */
  private String value = "some value";

  /**
   * Creates a new instance of ReflexiveInvocation
   */
  public ReflexiveInvocation() {
  }

  /**
   * Main demo method.
   * 
   * @param args
   *          the command line arguments
   * 
   * @throws RuntimeException
   *           __UNDOCUMENTED__
   */
  public static void main(final String[] args) {
    try {
      final int CALL_AMOUNT = 1000000;
      final ReflexiveInvocation ri = new ReflexiveInvocation();
      int idx = 0;

      // Call the method without using reflection.
      long millis = System.currentTimeMillis();

      for (idx = 0; idx < CALL_AMOUNT; idx++) {
        ri.getValue();
      }

      System.out.println("Calling method " + CALL_AMOUNT + " times programatically took "
          + (System.currentTimeMillis() - millis) + " millis");

      // Call while looking up the method in each iteration.
      Method md = null;
      millis = System.currentTimeMillis();

      for (idx = 0; idx < CALL_AMOUNT; idx++) {
        md = ri.getClass().getMethod("getValue", null);
        md.invoke(ri, null);
      }

      System.out.println("Calling method " + CALL_AMOUNT + " times reflexively with lookup took "
          + (System.currentTimeMillis() - millis) + " millis");

      // Call using a cache of the method.
      md = ri.getClass().getMethod("getValue", null);
      millis = System.currentTimeMillis();

      for (idx = 0; idx < CALL_AMOUNT; idx++) {
        md.invoke(ri, null);
      }

      System.out.println("Calling method " + CALL_AMOUNT + " times reflexively with cache took "
          + (System.currentTimeMillis() - millis) + " millis");
    } catch (final NoSuchMethodException ex) {
      throw new RuntimeException(ex);
    } catch (final InvocationTargetException ex) {
      throw new RuntimeException(ex);
    } catch (final IllegalAccessException ex) {
      throw new RuntimeException(ex);
    }
  }

  /**
   * Getter for property value.
   * 
   * @return Value of property value.
   */
  public String getValue() {
    return this.value;
  }
}

/* ########## End of File ########## */

 

   
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Class Reflection: class modifierClass Reflection: class modifier
2.Class Reflection: class nameClass Reflection: class name
3.Class Reflection: name for super classClass Reflection: name for super class
4.Object Reflection: create new instance
5.Class reflectionClass reflection
6.This class shows using Reflection to get a field from another classThis class shows using Reflection to get a field from another class
7.Show the class keyword and getClass() method in actionShow the class keyword and getClass() method in action
8.Simple Demonstration of a ClassLoader WILL NOT COMPILE OUT OF THE BOX
9.Demonstrate classFor to create an instance of an object
10.CrossRef prints a cross-reference about all classes named in argv
11.Make up a compilable version of a given Sun or other API
12.Show a couple of things you can do with a Class object
13.Reflect1 shows the information about the class named in argv
14.Show that you can, in fact, take the class of a primitive
15.JavaP prints structural information about classes
16.Provides a set of static methods that extend the Java metaobject
17.Use reflection to get console char set
18.Load the class source location from Class.getResource()
19.Access the enclosing class from an inner class
20.Use reflection to dynamically discover the capabilities of a class.
21.Get the class By way of a string
22.Get the class By way of .class
23.Return a String representation of an object's overall identity
24.Manipulate Java class files in strange and mysterious ways
25.Class file reader for obtaining the parameter names for declared methods in a class
26.Convert a given String into the appropriate Class.
27.Manipulate Java classes
28.Encapsulates a class serialVersionUID and codebase.
29.Dump a class using Reflection
30.This program uses reflection to spy on objects
31.This program uses reflection to print all features of a classThis program uses reflection to print all features of a class
32.Get Unqualified Name
33.Return a paranthesis enclosed, comma sepearated String of all SimpleClass names in params.
34.Adds the class SimpleNames, comma sepearated and surrounded by paranthesis to the call StringBuffer
35.Class Finder