相关疑难解决方法(0)

什么是反思,为什么它有用?

什么是反思,为什么它有用?

我对Java特别感兴趣,但我认为在任何语言中原则都是一样的.

java reflection terminology

2011
推荐指数
19
解决办法
85万
查看次数

Java反射性能

使用反射创建对象而不是调用类构造函数会导致任何显着的性能差异吗?

java reflection optimization performance

168
推荐指数
10
解决办法
10万
查看次数

Java反思:为什么这么慢?

基于其缓慢的声誉,我总是避免使用Java反射.我在当前项目的设计中达到了一个重点,能够使用它会使我的代码更具可读性和优雅性,所以我决定试一试.

我对这种差异感到惊讶,有时候我注意到运行时间差了近100倍.即使在这个简单的例子中它只是实例化一个空类,它也令人难以置信.

class B {

}

public class Test {

    public static long timeDiff(long old) {
        return System.currentTimeMillis() - old;
    }

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

        long numTrials = (long) Math.pow(10, 7);

        long millis;

        millis = System.currentTimeMillis();

        for (int i=0; i<numTrials; i++) {
            new B();
        }
        System.out.println("Normal instaniation took: "
                 + timeDiff(millis) + "ms");

        millis = System.currentTimeMillis();

        Class<B> c = B.class;

        for (int i=0; i<numTrials; i++) {
            c.newInstance();
        }

        System.out.println("Reflecting instantiation took:" 
              + timeDiff(millis) + "ms");

    } …
Run Code Online (Sandbox Code Playgroud)

java reflection performance

47
推荐指数
4
解决办法
2万
查看次数