我正在尝试衡量所需的时间" Dedup"一组随机的返回单词。如果我将maxWords设置为1000,它将测量重复一组10个单词所需的时间,并在每次迭代时将其递增10,直到达到1000个单词。时间似乎总是0,即使是1000字。我尝试过所有默认类型的重复数据删除(basicDedup,arrayListDedup等等)。
/*....*/
String[] samples;
Utils.Sampler sampler = new Utils.Sampler(readFile); //sampler with words to read
Utils.Stopwatch timer = new Utils.Stopwatch(); //Used to measure time
Utils.Output out = new Utils.Output(dedupType + ".csv"); //Makes file with the measurement
for (int i = 10; i <= maxWords; i += 10) {
int size = i; //Words to read for the iteration
samples = sampler.get(size); //Array containing the words for the iteration
long time = 0;
timer.start();
Dedup.treeSetDedup(samples);
time = timer.elapsedTime();
out.addMeasurement(size, time);
}
/*....*/
public static class Stopwatch {
private final ThreadMXBean timer;
private long start;
public Stopwatch(){
timer = ManagementFactory.getThreadMXBean();
start();
}
/**
* Start the timer
*/
public void start(){start = timer.getCurrentThreadCpuTime();}
/**
* Returns the time in nanoseconds since the last call <tt>start()</tt>.
*/
public long elapsedTime() {
long now = timer.getCurrentThreadCpuTime();
return now-start;
}
/**
* Runs the runnable object <tt>r</tt>, and returns the elapsed time
* measured in nanoseconds.
*/
public static long elapsedTime(Runnable runnable){
long start = System.nanoTime();
runnable.run();
long stop = System.nanoTime();
return stop-start;
}
/**
* Calls the given function with the given argument,
* and returns the elapsed time measured in nanoseconds.
*/
public static <T,R> long elapsedTime(Function<T,R> fu, T args){return elapsedTime(() -> fu.apply(args));}
}