com.xkzhangsan.time.cost.NanosecondCost Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xk-time Show documentation
Show all versions of xk-time Show documentation
xk-time is a datetime converter calculator and formatter tool set, based on java8 date and time API, thread safe, easy to use.
package com.xkzhangsan.time.cost;
/**
* 计算耗时,单位纳秒
*
* @author xkzhangsan
*/
public final class NanosecondCost implements Cost{
private final String name;
private final long nanosecond;
public NanosecondCost() {
super();
this.name = "";
this.nanosecond = System.nanoTime();
}
public NanosecondCost(String name) {
super();
this.name = name;
this.nanosecond = System.nanoTime();
}
@Override
public long stop() {
return System.nanoTime() - nanosecond;
}
@Override
public void stopAndPrint() {
System.out.println(stopAndFormat());
}
@Override
public String stopAndFormat() {
return (this.name + " cost: " + stop() + " ns");
}
@Override
public String stopAccurate() {
return String.valueOf(stop());
}
@Override
public void stopAccurateAndPrint() {
System.out.println(stopAccurateAndFormat());
}
@Override
public String stopAccurateAndFormat() {
return stopAndFormat();
}
}