代码之家  ›  专栏  ›  技术社区  ›  Scottie

为什么第一次呼叫的响应时间比以下时间长

  •  0
  • Scottie  · 技术社区  · 12 年前

    我在eclipse中通过Axis2代码生成器生成了存根。

    我的客户代码如下:

    ServerStub server = new ServerStub();
    
    for (int i = 0; i < 5; i++) {
        java.sql.Timestamp requestTime = new java.sql.Timestamp(System.currentTimeMillis());
    
        OperationResponse res = server.operation(new operation());
    
        System.out.println("Response Time: " + (System.currentTimeMillis() - requestTime.getTime()) + "ms");
    }
    

    然后我得到了输出:

    Response Time: 371ms
    Response Time: 13ms
    Response Time: 16ms
    Response Time: 14ms
    Response Time: 11ms
    

    有人能解释吗?谢谢。

    注:服务器和客户端位于同一网段中

    1 回复  |  直到 12 年前
        1
  •  2
  •   Damian Leszczyński - Vash    12 年前

    时间上的差异可能是因为类加载器的原因。当您第一次调用时,JVM需要准备可执行的代码。这个过程需要一些时间。要执行有效的时间测试,您应该首先“预热”JVM并加载所有必需的类。