wensiqun/asmsupport

what about the performance between asmsupport and javaassist ?

Closed this issue · 1 comments

what about the performance between asmsupport and javaassist ?

性能差不多,简单的创建一个Hello World略快,javassist可能还有部分语法分析的工作所以略慢。

import cn.wensiqun.asmsupport.client.DummyClass;
import cn.wensiqun.asmsupport.client.block.MethodBody;
import cn.wensiqun.asmsupport.client.def.var.LocVar;
import javassist.*;

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

public class PerformanceTest {

    public static void main(String... a) throws IOException, CannotCompileException, IllegalAccessException, InstantiationException, NoSuchMethodException,     InvocationTargetException {
        long start;
        Method method;
        Class clazz;

        start = System.currentTimeMillis();
        ClassPool cp = new ClassPool(true);
        CtClass ctClass = cp.makeClass("javassist.HelloWorld");
        ctClass.addMethod(CtNewMethod.make("public static void main(String[] args) {System.out.println(\"Hello World\");}",
                ctClass));
        clazz = ctClass.toClass();

        System.out.println(System.currentTimeMillis() - start);
        method = clazz.getDeclaredMethod("main", String[].class);
        method.invoke(clazz, new Object[]{null});



        start = System.currentTimeMillis();
        DummyClass dummyClass = new DummyClass("asmsupport.HelloWorld").public_();
        dummyClass.newMethod("main").argTypes(String[].class).public_().static_().body(new MethodBody() {
            @Override
            public void body(LocVar... args) {
                val(System.class).field("out").call("println", val("Hello World"));
                return_();
            }
        });
        clazz = dummyClass.build();

        System.out.println(System.currentTimeMillis() - start);
        method = clazz.getDeclaredMethod("main", String[].class);
        method.invoke(clazz, new Object[]{null});

    }

}