第二个方法调用比第一个调用时间少得多 [英] Second method call takes far less time than the first

查看:160
本文介绍了第二个方法调用比第一个调用时间少得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加速我的程序启动,并注意到我有一个方法有点奇怪。该程序本身是一个轻量级的Java游戏库应用程序,从文件中读取点,并呈现它们。注意的方法是从文件中读取的方法。

有什么好奇的方法是我在我的测试中调用了两次,第一次调用需要2837ms才能完成,而第二个只需要1704ms。



这两个调用之间的差异是最小的。不同之处在于第二次调用在开始对文件进行任何操作之前读取了一半的文件,而第一次调用在开始之前跳过一行。



这是所讨论的方法:

  private void initData(final int type)throws IOException {
final List< Vertex> top = new ArrayList< Vertex>();
final List< Vertex> bot = new ArrayList< Vertex>();
final BufferedReader input = new BufferedReader(new FileReader(location)); $(b)if(type == 1){
while(!input.readLine()。contains(lens));
} else {
input.readLine(); (input.ready()){
final Scanner in = new Scanner(input.readLine());

while(input.ready
while(in.hasNextFloat()){
final float x = in.nextFloat();
final float y = in.nextFloat();
top.add(new Vertex(x,y,in.nextFloat()));
bot.add(new Vertex(x,y,in.nextFloat()));
in.nextFloat(); (in.findInLine([lens])!= null)
||(in.findInLine([thin])!= null)
||(in.findInLine([thick])!= null)
||(in.findInLine([end])!= null)){
break;
}
}
input.close();
final long start = Diagnostics.getCurrentTime();
mergeAndSort(top,bot);
System.out.println(Sort:+(Diagnostics.getCurrentTime() - start));

$ / code $ / pre
$ b $我从这里得到的输出是:

 排序:15 
初始化:2836
排序:4
初始化:1704
读取信息:27

所有数字均以毫秒为单位。您会注意到在第二次运行期间,排序几乎需要四分之一的时间。每次运行时排序方法都是一样的。

方法所在的类是每次调用方法时新创建的。



所以,我很好奇为什么第一次调用比第二次调用花费了几乎整整一秒的时间。 解决方案处理文件的一半需要大约一半的时间来处理整个文件,这使得我相信大部分时间都花在了对象创建(Vertex类的实例化)上。

b
$ b

然而,真正确定发生什么事的唯一方法是使用分析工具。



JVisualVM,它是JDK发行版的一部分。

I'm trying to speed up the startup for my program, and noticed I had a method that was acting a little strange. The program itself is a Lightweight Java Game Library application that reads in points from a file, and renders them. The method of note is the method that reads from the file.

What's curious about the method is I call it twice during my tests, where the first call takes 2837ms to complete, and the second only takes 1704ms.

The difference between the two calls are minimal. The difference is the second call reads through half the file before beginning any operations with the file, while the first skips a single line before starting.

This is the method in question:

private void initData(final int type) throws IOException {
    final List<Vertex> top = new ArrayList<Vertex>();
    final List<Vertex> bot = new ArrayList<Vertex>();
    final BufferedReader input = new BufferedReader(new FileReader(location));
    if(type == 1) {
        while (!input.readLine().contains("lens"));
    } else {
        input.readLine();
    }
    while (input.ready()) {
        final Scanner in = new Scanner(input.readLine());
        while (in.hasNextFloat()) {
            final float x = in.nextFloat();
            final float y = in.nextFloat();
            top.add(new Vertex(x, y, in.nextFloat()));
            bot.add(new Vertex(x, y, in.nextFloat()));
            in.nextFloat();
        }
        if ((in.findInLine("[lens]") != null)
                || (in.findInLine("[thin]") != null)
                || (in.findInLine("[thick]") != null)
                || (in.findInLine("[end]") != null)) {
            break;
        }
    }
    input.close();
    final long start = Diagnostics.getCurrentTime();
    mergeAndSort(top, bot);
    System.out.println("Sort: " + (Diagnostics.getCurrentTime() - start));
}

The output I get from this is:

Sort: 15
Initializing: 2836
Sort: 4
Initializing: 1704
Reading info: 27

Where all numbers are in milliseconds. You'll notice the Sort takes almost a quarter of the time during the second run through. The sort method is identical each time it's run.

The class the methods are contained in is newly created each time the method is called.

So, I'm curious as to why the first call takes almost a full second longer than the second call.

解决方案

The fact that processing half of the file takes approximately half the time of processing the whole file leads me to believe that most of the time is spent in object creation (instantiation of the Vertex class).

However, the only way to be really sure what is going on is to use a profiling tool.

I'd Start with JVisualVM, which is part of the JDK distribution.

这篇关于第二个方法调用比第一个调用时间少得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆