为动作计时

发布于 2024-11-10 10:11:54 字数 68 浏览 4 评论 0原文

我正在通过一些操作来解析文件,并且我想测量执行这些操作所需的时间。

执行此操作并打印时间的最佳方法是什么?

I'm parsing a file with a few actions, and I want to measure the time that it takes to perform those actions.

What is the best way to do this and print the time?

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

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

发布评论

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

评论(5

孤君无依 2024-11-17 10:11:54

更新:如果您不反对使用外部库并且使用的是 JDK 5+,Google Guava 有一个秒表 使用 System.nanoTime()。它与绝对系统或挂钟时间无关,而是仅在计算经过的时间时有用,但这正是您想要的。

否则,您可以使用 System.currentTimeMillis() 以长整数形式返回当前系统时间(以毫秒为单位)。

long start = System.currentTimeMillis();
// ... perform operations ...
long time = System.currentTimeMillis() - start;
System.out.println("Operation took " + time + "ms");

对短期运行操作进行计时可能非常棘手,特别是当您在循环中运行它们以在多次运行中分摊时。您会受到操作系统任务调度程序和 Java 内部线程调度程序的影响。

为了缓解这些问题,请平均多次运行的时间,并在可能的情况下将控制台输出操作移出计时。请记住,您不会得到确切的数字。

Update: If you're not opposed to using external libraries and you're on JDK 5+, Google Guava has a Stopwatch that uses System.nanoTime(). It is not associated with absolute system or wall clock time but instead is useful only when calculating elapsed time, but this is exactly what you want.

Otherwise, you can use System.currentTimeMillis() which returns the current system time in milliseconds as a long integer.

long start = System.currentTimeMillis();
// ... perform operations ...
long time = System.currentTimeMillis() - start;
System.out.println("Operation took " + time + "ms");

Timing short-run operations can be very tricky, especially when you run them in a loop to amortize it across multiple runs. You're at the whims of the task scheduler of the operating system and Java's internal thread scheduler.

To mitigate these issues, average the time across multiple runs and move console output operations out of the timing if possible. Just keep in mind that you're not going to get exact numbers.

甜`诱少女 2024-11-17 10:11:54

这是通常测量执行时间的方法:

long start = System.currentTimeMillis();
//perform action
System.out.println("It took " + (System.currentTimeMillis() - start) + "ms");

This is how you usually measure execution time:

long start = System.currentTimeMillis();
//perform action
System.out.println("It took " + (System.currentTimeMillis() - start) + "ms");
孤独陪着我 2024-11-17 10:11:54

不确定你所说的动作是什么意思,但如果你想看看一段代码需要多长时间:

long start = System.nanoTime();
//Your code here
long timeElapsed = System.nanoTime() - start;
System.out.println(timeElapsed);

这将打印所花费的纳秒数

Not sure what you mean by actions, but if you want to see how long a piece of code takes:

long start = System.nanoTime();
//Your code here
long timeElapsed = System.nanoTime() - start;
System.out.println(timeElapsed);

That will print the number of nanoseconds taken

娇柔作态 2024-11-17 10:11:54

我会使用 IDE 的分析器或 jvisualvm< /代码>

I'd use my IDE's profiler or jvisualvm.

贵在坚持 2024-11-17 10:11:54
long start=System.currentTimeMillis();
//your action code
long end=System.currentTimeMillis();

int durationInMillis=end-start;
long start=System.currentTimeMillis();
//your action code
long end=System.currentTimeMillis();

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