http://www.qqread.com/netbase/w478517.html 


JDK 6和CGLib cglib-nodep-2.2.jar对比结果:


JDK Proxy: 1,049,937 calls/s


CGLIB: 2,820,130 calls/s


如果使用cglib以前版本,性能更快:


JDK Proxy: 1,037,575 calls/s


CGLIB: 3,112,727 calls/s


而JDK 6和JavaAssit 3.11测试结果如下:


JDK Proxy: 1,037,575 calls/s


JAVAASSIST: 626,695 calls/s 

 

从数据上看,cglib性能最好。但是和ASM、直接调用比较还不知道。 

public class ProxyPerformanceComparison2 {

public static void main(String[] args) throws Exception {

Callable
<integer> jdkProxy = (Callable<integer>)

Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), 
new Class[] {

Callable.
class },

new JdkHandler(new Counter()));

ProxyFactory f 
= new ProxyFactory();

f.setInterfaces(
new Class[] { Callable.class });

Class c 
= f.createClass();

Callable
<integer> cglibProxy = (Callable<integer>) c.newInstance();

((ProxyObject) cglibProxy).setHandler

(
new JavaAssitInterceptor(new Counter()));

for (int i2 = 0; i2 < 10; i2++) {

iterate(jdkProxy, 
"JDK Proxy: ");

iterate(cglibProxy, 
"JAVAASSIST: ");

System.err.println();

}

}

static final DecimalFormat format = new DecimalFormat();

static void iterate(Callable<integer> callable, String label)

throws Exception {

int count = 10000000;

long time = System.currentTimeMillis();

int total = 0;

for (int i = 0; i < count; i++) {

total 
+= callable.call();

}

time 
= System.currentTimeMillis() - time;

System.err.println(label 
+ format.format

(count 
* 1000 / time) + " calls/s");

}

static class JdkHandler implements InvocationHandler {

final Object delegate;

JdkHandler(Object delegate) {

this.delegate = delegate;

}

public Object invoke

(Object object, Method method, Object[] objects) 
throws Throwable {

return method.invoke(delegate, objects);

}

}

static class JavaAssitInterceptor implements MethodHandler {

final Object delegate;

JavaAssitInterceptor(Object delegate) {

this.delegate = delegate;

}

public Object invoke

(Object self, Method m, Method proceed, Object[] args) 
throws Throwable {

return m.invoke(delegate, args);

}

}

static class Counter implements Callable<integer> {

int count = 0;

public Integer call() throws Exception {

return count++;

}

}

}

相关文章:

  • 2021-11-20
  • 2021-06-23
  • 2022-02-28
  • 2021-11-27
  • 2021-11-17
猜你喜欢
  • 2021-12-27
  • 2022-01-10
相关资源
相似解决方案