-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTiming.java
More file actions
73 lines (57 loc) · 2.24 KB
/
Timing.java
File metadata and controls
73 lines (57 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class Timing {
private StringBuilder output;
private long previousDimension;
private long cumulativeEvaluations;
private long startTime;
private long overallStartTime;
/**
* Constructor
*/
public Timing() {
super();
this.output = new StringBuilder();
this.previousDimension = 0;
this.cumulativeEvaluations = 0;
this.startTime = System.nanoTime();
this.overallStartTime = this.startTime;
}
/**
* Keeps track of the total number of evaluations and elapsed time. Produces an output string when the
* current problem is of a different dimension than the previous one or when null.
*/
void timeProblem(Problem problem) {
if ((problem == null) || (this.previousDimension != CocoJNI.cocoProblemGetDimension(problem.getPointer()))) {
/* Output existing timing information */
if (this.cumulativeEvaluations > 0) {
long elapsedTime = System.nanoTime() - this.startTime;
String elapsed = String.format(Locale.ENGLISH, "%.2e", elapsedTime / (1.0 * 1e+9) / (1.0 * this.cumulativeEvaluations));
this.output.append("d=" + this.previousDimension + " done in " + elapsed + " seconds/evaluation\n");
}
if (problem != null) {
/* Re-initialize the timing_data */
this.previousDimension = CocoJNI.cocoProblemGetDimension(problem.getPointer());
this.cumulativeEvaluations = CocoJNI.cocoProblemGetEvaluations(problem.getPointer());
this.startTime = System.nanoTime();
}
} else {
this.cumulativeEvaluations += CocoJNI.cocoProblemGetEvaluations(problem.getPointer());
}
}
/**
* Outputs the collected timing data.
*/
void output() {
/* Record the last problem */
timeProblem(null);
long elapsedTime = System.nanoTime() - this.overallStartTime;
long hours = TimeUnit.HOURS.convert(elapsedTime, TimeUnit.NANOSECONDS);
long minutes = TimeUnit.MINUTES.convert(elapsedTime, TimeUnit.NANOSECONDS) - hours * 60;
long seconds = TimeUnit.SECONDS.convert(elapsedTime, TimeUnit.NANOSECONDS) - hours * 3600 - minutes * 60;
String elapsed = String.format("Total elapsed time: %dh%02dm%02ds\n", hours, minutes, seconds);
this.output.insert(0, "\n");
this.output.append(elapsed);
System.out.append(this.output.toString());
}
}