简体   繁体   English

如何准确计算Java程序写入或读取文件所需的时间?

[英]How to compute accurately the time it takes a Java program to write or read a file?

How to compute accurately the time it takes a Java program to write or read a number of bytes from/to a file ? 如何准确计算Java程序从/向文件写入或读取多个字节所需的时间?

It is really important that the time is being measured accurately. 准确测量时间非常重要。 (The time should be computed by the program itself). (时间应由程序本身计算)。

The standard idiom is: 标准习语是:

long startTime = System.nanoTime();
doSomething();
long elapsedTime = System.nanoTime() - startTime;

not tested, but something like: 未经测试,但有类似的事情:

long delta = System.nanoTime();
try {
    // do your stuff
} finally {
    delta = System.nanoTime() - delta;
}

There is a code sample here: 这里有一个代码示例:

http://www.goldb.org/stopwatchjava.html http://www.goldb.org/stopwatchjava.html

/*
    Copyright (c) 2005, Corey Goldberg

    StopWatch.java is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
*/


public class StopWatch {

    private long startTime = 0;
    private long stopTime = 0;
    private boolean running = false;


    public void start() {
        this.startTime = System.currentTimeMillis();
        this.running = true;
    }


    public void stop() {
        this.stopTime = System.currentTimeMillis();
        this.running = false;
    }


    //elaspsed time in milliseconds
    public long getElapsedTime() {
        long elapsed;
        if (running) {
             elapsed = (System.currentTimeMillis() - startTime);
        }
        else {
            elapsed = (stopTime - startTime);
        }
        return elapsed;
    }


    //elaspsed time in seconds
    public long getElapsedTimeSecs() {
        long elapsed;
        if (running) {
            elapsed = ((System.currentTimeMillis() - startTime) / 1000);
        }
        else {
            elapsed = ((stopTime - startTime) / 1000);
        }
        return elapsed;
    }




    //sample usage
    public static void main(String[] args) {
        StopWatch s = new StopWatch();
        s.start();
        //code you want to time goes here
        s.stop();
        System.out.println("elapsed time in milliseconds: " + s.getElapsedTime());
    }
}

The way I would do that is just run it in a loop some number of times. 我这样做的方法就是在循环中运行它多次。 Like if you run it 1000 times and clock it, that gives you milliseconds. 就像你运行它1000次并计时一样,这给你毫秒。 Run it 1,000,000 times, and it gives you microseconds. 运行1,000,000次,它给你微秒。

If you also want to find out why it's taking as long as it is, you can just pause it some number of times (like 10) while it's running, and that will tell you what it's doing and why. 如果你还想知道为什么它会花费多长时间,你可以在它运行时暂停一些次数(比如10),这会告诉你它在做什么以及为什么。

The problem with the get System.xxx method is that the method itself needs a few milliseconds to compute. get System.xxx方法的问题在于该方法本身需要几毫秒来计算。 The usually "accepted" way of doing it is running the test a few tens of thousands of times and calculating an average of this. 通常“接受”的方式是运行测试几万次并计算平均值。

Also, depending on your OS there is something called the time granularity (example for windows). 此外,根据您的操作系统,有一些称为时间粒度 (Windows的例子)。 This is the smallest amount of time your OS can compute. 这是您的操作系统可以计算的最短时间。 On some OS its a millisecond, on some others its a nanosecond. 在一些操作系统上它是一毫秒,在另一些操作系统上它是一个纳秒。 It might or might not be relevant in your case. 它可能与您的情况有关,也可能不相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM