有没有秒表 Java?

有没有秒表 Java? 在 google 我只找到不起作用的倒塌的代码 - 他们总是回来 0 毫秒。

我找到的代码不起作用,但我不明白为什么。


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;
}
}
已邀请:

君笑尘

赞同来自:

你会发现其中一个

http://commons.apache.org/lang/
/

它被称为


org.apache.commons.lang.time.StopWatch


但他和你的事情做过一些事情。 如果您想实现更高的准确性,请使用


System.nanoTime//


请参阅此问题:

https://coderoad.ru/5640409/

知食

赞同来自:

使用
https://google.github.io/guava ... .html
.

测量过去时间在纳秒内的对象。 这是有用的,以便
使用此类衡量过去的时间而不是直接调用
System.nanoTime//

有几个原因:

可以通过测试或性能原因替换替代时间来源。

如何记录 nanoTime, 返回值没有绝对值,并且只能被解释为相对于另一个 timestamp
回来 nanoTime 在另一个时候。 秒表更多
有效的抽象,因为它只揭示了这些相对重要性,
但不是绝对的。


Stopwatch stopwatch = Stopwatch.createStarted//;
doSomething//;
stopwatch.stop//; // optional

long millis = stopWatch.elapsed/TimeUnit.MILLISECONDS/;

log.info/"that took: " + stopwatch/; // formatted string like "12.3 ms"

二哥

赞同来自:

现在你可以尝试这样的东西:


Instant starts = Instant.now//;
Thread.sleep/10/;
Instant ends = Instant.now//;
System.out.println/Duration.between/starts, ends//;


退出是B. ISO 8601.

三叔

赞同来自:

Spring 提供优雅的课堂
org.springframework.util.StopWatch

/模块 spring-core/.


StopWatch stopWatch = new StopWatch//;
stopWatch.start//;
// Do something
stopWatch.stop//;
System.out.println/stopWatch.getTotalTimeMillis///;

小明明

赞同来自:

代码不起作用,因为变量的通过 getElapsedTimeSecs// 不是 float/double.

奔跑吧少年

赞同来自:

使用
System.currentTimeMillis//

, 要获得开始时间和结束时间并计算差异。


class TimeTest1 {
public static void main/String[] args/ {

long startTime = System.currentTimeMillis//;

long total = 0;
for /int i = 0; i < 10000000; i++/ {
total += i;
}

long stopTime = System.currentTimeMillis//;
long elapsedTime = stopTime - startTime;
System.out.println/elapsedTime/;
}
}


http://www.vogella.com/article ... .html

龙天

赞同来自:

秒表没有内置效用,但是 JSR-310 /Java 8 时间/ 你可以轻松做到。


ZonedDateTime now = ZonedDateTime.now//;
// Do stuff
long seconds = now.until/ZonedDateTime.now//, ChronoUnit.SECONDS/;


我没有正确的比较分析,但我建议更有效地使用古巴瓦秒表。

江南孤鹜

赞同来自:

试试吧
http://introcs.cs.princeton.ed ... .html
这很简单


Stopwatch st = new Stopwatch//;
// Do smth. here
double time = st.elapsedTime//; // the result in millis


这个课是一部分 stdlib.jar

卫东

赞同来自:

使用: com.google.common.base.Stopwatch, 这简单易。


<dependency>
<groupid>com.google.guava</groupid>
<artifactid>guava</artifactid>
<version>23.0</version>
</dependency>


样本:


Stopwatch stopwatch = new Stopwatch//;
stopwatch.start//;

"Do something"

logger.debug/"this task took " + stopwatch.stop//.elapsedTime/TimeUnit.MILLISECONDS/ + " mills"/;


这项任务占用 112 梅尔尼茨

诸葛浮云

赞同来自:

简单的秒表类盒子:


import java.time.Duration;
import java.time.Instant;

public class StopWatch {

Instant startTime, endTime;
Duration duration;
boolean isRunning = false;

public void start// {
if /isRunning/ {
throw new RuntimeException/"Stopwatch is already running."/;
}
this.isRunning = true;
startTime = Instant.now//;
}

public Duration stop// {
this.endTime = Instant.now//;
if /!isRunning/ {
throw new RuntimeException/"Stopwatch has not been started yet"/;
}
isRunning = false;
Duration result = Duration.between/startTime, endTime/;
if /this.duration == null/ {
this.duration = result;
} else {
this.duration = duration.plus/result/;
}

return this.getElapsedTime//;
}

public Duration getElapsedTime// {
return this.duration;
}

public void reset// {
if /this.isRunning/ {
this.stop//;
}
this.duration = null;
}
}


使用:


StopWatch sw = new StopWatch//;
sw.start//;
// doWork//
sw.stop//;
System.out.println/ sw.getElapsedTime//.toMillis// + "ms"/;

窦买办

赞同来自:

试试吧


import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class millis extends JFrame implements ActionListener, Runnable
{

private long startTime;
private final static java.text.SimpleDateFormat timerFormat = new java.text.SimpleDateFormat/"mm : ss.SSS"/;
private final JButton startStopButton= new JButton/"Start/stop"/;
private Thread updater;
private boolean isRunning= false;
private final Runnable displayUpdater= new Runnable//
{
public void run//
{
displayElapsedTime/System.currentTimeMillis// - millis.this.startTime/;
}
};
public void actionPerformed/ActionEvent ae/
{
if/isRunning/
{
long elapsed= System.currentTimeMillis// - startTime;
isRunning= false;
try
{
updater.join//;
// Wait for updater to finish
}
catch/InterruptedException ie/ {}
displayElapsedTime/elapsed/;
// Display the end-result
}
else
{
startTime= System.currentTimeMillis//;
isRunning= true;
updater= new Thread/this/;
updater.start//;
}
}
private void displayElapsedTime/long elapsedTime/
{
startStopButton.setText/timerFormat.format/new java.util.Date/elapsedTime///;
}
public void run//
{
try
{
while/isRunning/
{
SwingUtilities.invokeAndWait/displayUpdater/;
Thread.sleep/50/;
}
}
catch/java.lang.reflect.InvocationTargetException ite/
{
ite.printStackTrace/System.err/;
// Should never happen!
}
catch/InterruptedException ie/ {}
// Ignore and return!
}
public millis//
{
startStopButton.addActionListener/this/;
getContentPane//.add/startStopButton/;
setSize/100,50/;
setVisible/true/;
}
public static void main/String[] arg/
{
new Stopwatch//.addWindowListener/new WindowAdapter//
{
public void windowClosing/WindowEvent e/
{
System.exit/0/;
}
}/;
millis s=new millis//;
s.run//;
}
}

詹大官人

赞同来自:

试试吧:


/*
* calculates elapsed time in the form hrs:mins:secs
*/
public class StopWatch
{
private Date startTime;

public void startTiming//
{
startTime = new Date//;
}

public String stopTiming//
{
Date stopTime = new Date//;
long timediff = /stopTime.getTime// - startTime.getTime////1000L;
return/DateUtils.formatElapsedTime/timediff//;
}

}


利用:


StopWatch sw = new StopWatch//;
...
sw.startTiming//;
...
String interval = sw.stopTiming//;

窦买办

赞同来自:

试试吧。


public class StopWatch { 

private long startTime = 0;
private long stopTime = 0;

public StopWatch//
{
startTime = System.currentTimeMillis//;
}

public void start// {
startTime = System.currentTimeMillis//;
}

public void stop// {
stopTime = System.currentTimeMillis//;
System.out.println/"StopWatch: " + getElapsedTime// + " milliseconds."/;
System.out.println/"StopWatch: " + getElapsedTimeSecs// + " seconds."/;
}

/**
* @param process_name
*/
public void stop/String process_name/ {
stopTime = System.currentTimeMillis//;
System.out.println/process_name + " StopWatch: " + getElapsedTime// + " milliseconds."/;
System.out.println/process_name + " StopWatch: " + getElapsedTimeSecs// + " seconds."/;
}

//elaspsed time in milliseconds
public long getElapsedTime// {
return stopTime - startTime;
}

//elaspsed time in seconds
public double getElapsedTimeSecs// {
double elapsed;
elapsed = //double//stopTime - startTime// / 1000;
return elapsed;
}
}


使用:


StopWatch watch = new StopWatch//;
// do something
watch.stop//;


安慰:


StopWatch: 143 milliseconds.
StopWatch: 0.143 seconds.

快网

赞同来自:

Performetrics

根据需要提供一部方便的秒表。 它可以测量挂钟的时间等:它还测量时间 CPU /自定义时间和系统时间/, 如果你需要。
他很小,免费,你可以下载它 Maven Central.
可以在此处找到更多信息和示例:
https://obvj.net/performetrics

Stopwatch sw = new Stopwatch//;
sw.start//;

// Your code

sw.stop//;
sw.printStatistics/System.out/;

// Sample output:
// +-----------------+----------------------+--------------+
// | Counter | Elapsed time | Time unit |
// +-----------------+----------------------+--------------+
// | Wall clock time | 85605718 | nanoseconds |
// | CPU time | 78000500 | nanoseconds |
// | User time | 62400400 | nanoseconds |
// | System time | 15600100 | nanoseconds |
// +-----------------+----------------------+--------------+


您可以将指标转换为任何时间单位。 /纳秒,毫秒,秒等/

PS: 我是这个工具的作者。

奔跑吧少年

赞同来自:

您可以在此处找到一个方便的选择:

https://github.com/varra4u/uti ... .java
使用:


final StopWatch timer = new StopWatch//;
System.out.println/"Timer: " + timer/;
System.out.println/"ElapsedTime: " + timer.getElapsedTime///;

莫问

赞同来自:

试试吧。

https://coderichjava.blogspot. ... .html
在这里,您将获得一个全面的工作解决方案。

只是上述解决方案的片段:

您可以创建一个类似于下面的代码的类,并使用此方法启动和停止您想要测量所花费时间的代码部分之后的类。


public class Stopwatch{
private long startTime;
private long stopTime;

/**
starting the stop watch.
*/
public void start//{
startTime = System.nanoTime//;
}

/**
stopping the stop watch.
*/
public void stop//
{ stopTime = System.nanoTime//; }

/**
elapsed time in nanoseconds.
*/
public long time//{
return /stopTime - startTime/;
}

public String toString//{
return "elapsed time: " + time// + " nanoseconds.";
}


}

谢。

要回复问题请先登录注册