我想要这样的东西:
public class Stream
{
public startTime;
public endTime;
public getDuration()
{
return startTime - endTime;
}
}
同样重要的是,例如,如果startTime是23:00,endTime是1:00,则持续时间为2:00。
为了在Java中实现这一点,应该使用哪些类型?
我想要这样的东西:
public class Stream
{
public startTime;
public endTime;
public getDuration()
{
return startTime - endTime;
}
}
同样重要的是,例如,如果startTime是23:00,endTime是1:00,则持续时间为2:00。
为了在Java中实现这一点,应该使用哪些类型?
当前回答
为了在Java中实现这一点,应该使用哪些类型?
简短的回答是长。现在,更多关于如何衡量……
System.currentTimeMillis()
“传统”的方法是使用System.currentTimeMillis():
long startTime = System.currentTimeMillis();
// ... do something ...
long estimatedTime = System.currentTimeMillis() - startTime;
o.a.c.l.t.StopWatch
注意,Commons Lang有一个StopWatch类,可以用来测量以毫秒为单位的执行时间。它有split(), suspend(), resume()等方法,允许在执行的不同点采取措施,您可能会发现方便。看一看。
System.nanoTime()
如果您正在寻找运行时间的极其精确的度量,您可能更喜欢使用System.nanoTime()。从它的javadoc:
long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;
Jamon
另一种选择是使用JAMon,这是一种收集start()和stop()方法之间任何代码的统计信息(执行时间、命中次数、平均执行时间、最小值、最大值等)的工具。下面是一个非常简单的例子:
import com.jamonapi.*;
...
Monitor mon=MonitorFactory.start("myFirstMonitor");
...Code Being Timed...
mon.stop();
查看www.javaperformancetunning.com上的这篇文章,有很好的介绍。
使用AOP
最后,如果您不想让这些度量使您的代码变得混乱(或者如果您不能更改现有的代码),那么AOP将是一个完美的武器。我不打算深入讨论这个问题但我至少想提一下。
下面是一个使用AspectJ和JAMon的非常简单的方面(这里,切入点的短名称将用于JAMon监视器,因此调用thisJoinPoint.toShortString()):
public aspect MonitorAspect {
pointcut monitor() : execution(* *.ClassToMonitor.methodToMonitor(..));
Object arround() : monitor() {
Monitor monitor = MonitorFactory.start(thisJoinPoint.toShortString());
Object returnedObject = proceed();
monitor.stop();
return returnedObject;
}
}
切入点定义可以很容易地调整为基于类名、包名、方法名或它们的任何组合来监视任何方法。度量实际上是AOP的一个完美用例。
其他回答
为了在Java中实现这一点,应该使用哪些类型?
答:长
public class Stream {
public long startTime;
public long endTime;
public long getDuration() {
return endTime - startTime;
}
// I would add
public void start() {
startTime = System.currentTimeMillis();
}
public void stop() {
endTime = System.currentTimeMillis();
}
}
用法:
Stream s = ....
s.start();
// do something for a while
s.stop();
s.getDuration(); // gives the elapsed time in milliseconds.
这是我对你第一个问题的直接回答。
最后一个“注释”,我建议你使用Joda Time。它包含一个适合您需要的间隔类。
如果你喜欢使用Java的日历API,你可以试试这个,
Date startingTime = Calendar.getInstance().getTime();
//later on
Date now = Calendar.getInstance().getTime();
long timeElapsed = now.getTime() - startingTime.getTime();
不幸的是,目前发布的十个答案中没有一个是完全正确的。
如果您正在测量经过的时间,并且希望它是正确的,那么必须使用System.nanoTime()。您不能使用System.currentTimeMillis(),除非您不介意结果是错误的。
The purpose of nanoTime is to measure elapsed time, and the purpose of currentTimeMillis is to measure wall-clock time. You can't use the one for the other purpose. The reason is that no computer's clock is perfect; it always drifts and occasionally needs to be corrected. This correction might either happen manually, or in the case of most machines, there's a process that runs and continually issues small corrections to the system clock ("wall clock"). These tend to happen often. Another such correction happens whenever there is a leap second.
由于nanoTime的目的是测量经过的时间,因此它不受任何这些小修正的影响。它是你想要使用的。当前使用currentTimeMillis进行的任何计时都将关闭——甚至可能为负值。
您可能会说,“这听起来似乎并没有那么重要”,对此我说,也许不是,但总的来说,正确的代码不就是比错误的代码好吗?此外,nanoTime打字时间更短。
之前发布的关于nanoTime通常只有微秒精度的免责声明是有效的。此外,它可能需要超过一微秒的时间来调用,这取决于环境(另一个也是如此),所以不要期望对非常非常小的间隔进行正确的计时。
我发现这段代码在计时时很有用:
public class Et {
public Et() {
reset();
}
public void reset() {
t0=System.nanoTime();
}
public long t0() {
return t0;
}
public long dt() {
return System.nanoTime()-t0();
}
public double etms() {
return etms(dt());
}
@Override public String toString() {
return etms()+" ms.";
}
public static double etms(long dt) {
return dt/1000000.; // 1_000_000. breaks cobertura
}
private Long t0;
}
你的新类:
public class TimeWatch {
long starts;
public static TimeWatch start() {
return new TimeWatch();
}
private TimeWatch() {
reset();
}
public TimeWatch reset() {
starts = System.currentTimeMillis();
return this;
}
public long time() {
long ends = System.currentTimeMillis();
return ends - starts;
}
public long time(TimeUnit unit) {
return unit.convert(time(), TimeUnit.MILLISECONDS);
}
}
用法:
TimeWatch watch = TimeWatch.start();
// do something
long passedTimeInMs = watch.time();
long passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
之后,所经过的时间可以转换为任何你喜欢的格式,例如日历
Greetz, 查德