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

发布于 2024-07-16 21:22:28 字数 78 浏览 4 评论 0原文

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

准确测量时间非常重要。 (时间应该由程序本身计算)。

How to compute accurately the time it takes a Java program to write or read a number of bytes from/to a file ?

It is really important that the time is being measured accurately. (The time should be computed by the program itself).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹿! 2024-07-23 21:22:28

标准的习语是:

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

The standard idiom is:

long startTime = System.nanoTime();
doSomething();
long elapsedTime = System.nanoTime() - startTime;
病女 2024-07-23 21:22:28

未经测试,但类似:

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

not tested, but something like:

long delta = System.nanoTime();
try {
    // do your stuff
} finally {
    delta = System.nanoTime() - delta;
}
苦妄 2024-07-23 21:22:28

这里有一个代码示例:

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());
    }
}

There is a code sample here:

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());
    }
}
回梦 2024-07-23 21:22:28

我这样做的方法就是循环运行它几次。 就像如果你运行它 1000 次并对其进行计时,就会得到毫秒数。 运行它 1,000,000 次,它会给出微秒。

如果您还想找出为什么它花了那么长时间,您可以在它运行时暂停它几次(例如 10 次),这会告诉您它在做什么以及为什么。

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. Run it 1,000,000 times, and it gives you microseconds.

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.

栀子花开つ 2024-07-23 21:22:28

get System.xxx 方法的问题在于该方法本身需要几毫秒的时间来计算。 通常“接受”的方法是运行测试数万次并计算平均值。

另外,根据您的操作系统,有一种称为时间粒度的东西(以 Windows 为例)。 这是操作系统可以计算的最短时间。 在某些操作系统上它是毫秒,在其他操作系统上它是纳秒。 它可能与您的情况相关,也可能不相关。

The problem with the get System.xxx method is that the method itself needs a few milliseconds to compute. 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). 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文