[E-Lang] Reflection invoke() timing measurement

Michael Malcolm michael.malcolm@agorics.com
Wed, 18 Jul 2001 17:56:04 -0700


Mark Miller asked me to post my measurement of the java reflection
invoke() method. In summary, using Java 1.3 on a 350MHz PentiumII
machine, each invoke call took a bit more than 5.5 microseconds, or
nearly 2000 machine cycles.

Michael Malcolm



Here is the code I used:

=============================================================================

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

class InvokeTimer {

    /**
    If an arg is supplied, it is the number of times to loop;
    if absent, 100K is used.
    */
    static public void
    main(String[] args)
            throws  IllegalAccessException,
                    NoSuchMethodException,
                    InvocationTargetException
    {
        Integer argN = args.length == 0 ? null : new Integer(args[0]);
        int n = null == argN ? 100000 : argN.intValue();

        Method m = System.class.getMethod("currentTimeMillis", new
Class[0]);

        long t0 = System.currentTimeMillis();
        for(int i = 0; i < n; i++) {
            m.invoke(System.class, null);
        }
        long t1 = System.currentTimeMillis();
        long invokeLoopTime = t1 - t0;

        long t00 = System.currentTimeMillis();
        for(int i = 0; i < n; i++) {
            System.currentTimeMillis();
        }
        long t01 = System.currentTimeMillis();
        long callLoopTime = t01 - t00;

        double usecsPer = (invokeLoopTime - callLoopTime) * 1000.0 / n;
        System.out.println("Microseconds per invoke: " + (float)
usecsPer);
    }
}
=============================================================================