> 
> Now that you have this model, realize that with Velocity, you could have
> avoided the entire exercise.  I think it will become clear why the
> WM/Velocity way of doing things is much preferred.
> 
> John McNally


I decided to test this quickly and wrote the test class below.

This probably doesn't prove anything but:
Tests 1 and 3 which are fairly nonsensical show direct method to be 200X
faster than reflection.

Test 2 includes a String concatenation in the method body and direct
method is 7X faster.

Test 4 is fairly representative of many reflection calls where we are
trying to display a simple property of an object.  And it shows the
direct method to be 100X faster.

Please feel free to show me my errors.  But the article is probably
correct in stating that FreeMarker is more efficient.  And I also still
think the development time savings is worth it.

John McNally


import java.lang.reflect.*;

public class MethodClass
{
        private String sp;

        public MethodClass()
        {
                sp = "adsf";
        }

        public String getSp()
        {
                return sp;
        }

        public String method1(String s)
        {
                return s;
        }

        public String method1(String s1, String s2)
        {
                return s1 + s2;
        }

        public String method2(MethodClass mc)
        {
                return mc.method1("called method1 from method2");
        }

        public static void main(String[] args)
                throws Exception
        {
                String s1 = "I'm s1";
                String s2 = "I'm s2";
                String result = null;
                int num = 1000000;

                Class mcClass = Class.forName("MethodClass");
                MethodClass mc = (MethodClass)mcClass.newInstance();


                Class[] sig = new Class[1];
                sig[0] = s1.getClass();
                Object[] objs = new Object[1];
                objs[0] = s1;

                Method m = mcClass.getMethod("method1", sig);

                long start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = (String)m.invoke(mc, objs);
                }
                long end = System.currentTimeMillis();

                long invokeTime = end - start;
                System.out.println("Invoke time: " + invokeTime);

                start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = mc.method1(s1);
                }
                end = System.currentTimeMillis();

                long directTime = end - start;
                System.out.println("Direct time: " + directTime);

// try second signature

                sig = new Class[2];
                sig[0] = s1.getClass();
                sig[1] = s2.getClass();
                objs = new Object[2];
                objs[0] = s1;
                objs[1] = s2;

                m = mcClass.getMethod("method1", sig);

                start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = (String)m.invoke(mc, objs);
                }
                end = System.currentTimeMillis();

                invokeTime = end - start;
                System.out.println("Invoke time: " + invokeTime);

                start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = mc.method1(s1,s2);
                }
                end = System.currentTimeMillis();

                directTime = end - start;
                System.out.println("Direct time: " + directTime);


// try third method
                sig = new Class[1];
                sig[0] = mc.getClass();
                objs = new Object[1];
                objs[0] = mc;

                m = mcClass.getMethod("method2", sig);

                start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = (String)m.invoke(mc, objs);
                }
                end = System.currentTimeMillis();

                invokeTime = end - start;
                System.out.println("Invoke time: " + invokeTime);

                start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = mc.method1(s1);
                }
                end = System.currentTimeMillis();

                directTime = end - start;
                System.out.println("Direct time: " + directTime);
// try getter

                sig = new Class[0];
                objs = new Object[0];

                m = mcClass.getMethod("getSp",null);

                start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = (String)m.invoke(mc, null);
                }
                end = System.currentTimeMillis();

                invokeTime = end - start;
                System.out.println("Invoke time: " + invokeTime);

                start = System.currentTimeMillis();
                for (int i=0; i<num; i++)
                {
                        result = mc.getSp();
                }
                end = System.currentTimeMillis();

                directTime = end - start;
                System.out.println("Direct time: " + directTime);
        }
}


------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to