RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 720339
Accepted
Direct
Direct
Asked:2020-09-19 19:15:51 +0000 UTC2020-09-19 19:15:51 +0000 UTC 2020-09-19 19:15:51 +0000 UTC

调用第一个类方法比调用后续方法要长得多。为什么?

  • 772

下面是我们使用动态代理(替代)来测量方法调用时间的代码。
问题是这样的:
我们的代理吞下一个real类的对象RealObject,然后SympleDynamicProxy.consumer(Interface iface)依次调用方法real.doSomething(),然后real.somethingElse(String arg)(我的意思是我们测量具体调用的时间“ REAL.method”,在这个意义上代理仍然有引用real)。最奇怪的是,虽然方法doSomething比较简单,但是耗时要长很多,请解释一下为什么?第一次调用创建的对象时会发生什么样的熨斗预热?毕竟,类已经加载,对象已经在堆上。第一次通话花费的时间是多少?即使somethingElse方法“更长”,为什么后续调用要快得多?


在输出中,我们看到我在自己
doSomething()身上花费了 ~ 265,000 ns
somethingElse()~ 45,000 ns

import java.lang.reflect.*;
import static lib.Printer.*;

interface Interface {
    void doSomething();

    void somethingElse(String arg);
}

class RealObject implements Interface {

    @Override
    public void doSomething() {
        print("doSomething");
    }

    @Override
    public void somethingElse(String arg) {
        print("Something else: " + arg);
    }

}

class DynamicProxyHandler implements InvocationHandler {
    private Object proxied;

    public DynamicProxyHandler(Object proxied) {
        this.proxied = proxied;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("**** proxy: " + proxy.getClass() + ", method: " + method + ", args: " + args);
        if (args != null)
            for (Object arg : args)
                System.out.println("  " + arg);
        Object o;
        long st = System.nanoTime();
        o = method.invoke(proxied, args);
        System.out.println("  spent: " + (System.nanoTime() - st));
        return o;
    }
}

class SimpleDynamicProxy {
    public static void consumer(Interface iface) {
        iface.doSomething();
        iface.somethingElse("bonobo");
    }

    public static void main(String[] args) {
        RealObject real = new RealObject();
        consumer(real);
        // Замеряем время:
        Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
                new Class[] { Interface.class }, new DynamicProxyHandler(real));
        consumer(proxy);
    }
}
java
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    angry
    2020-09-20T09:48:45Z2020-09-20T09:48:45Z

    该方法doSomething需要更长的时间来调用,因为这是第一次使用反射调用该方法。此时,作为第一次调用构造函数或静态方法的结果,加载了为此所需的类文件(缓存了 MB 的内容)。您可以通过在第一次调用之前和之后jmap从您的程序代码调用该实用程序来验证这一点。Method.invoke在这种情况下,我们将调用哪个方法并不重要。例如,让我们采取Object.toString.

    在代码的原始版本中,我们得到第一种方法比第二种方法花费的时间多 2 倍。现在让我们添加一个带有额外调用的行:

    public static void main(String[] args) {
        RealObject real = new RealObject();
        consumer(real); // <-- это не вызывается через reflection
    
        // добавляем...
        try {
            Object.class.getDeclaredMethod("toString").invoke(new Object());
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        // Замеряем время:
        Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
                new Class[] { Interface.class }, new DynamicProxyHandler(real));
        consumer(proxy);
    }
    

    并且方法的执行时间变得大致相同。

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5