-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfiler.java
55 lines (45 loc) · 1.42 KB
/
Profiler.java
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
import java.util.function.Function;
public class Profiler {
public static double analyse(Function<Double, Double> oneMethod, double p) {
long clock0 = timestamp();
double result = oneMethod.apply(p);
System.out.println("Temps d'exécution : " + timestamp(clock0));
return result;
}
public static boolean analyse2(Function<Double, Boolean> oneMethod, double p) {
long clock0 = timestamp();
boolean result = oneMethod.apply(p);
System.out.println("Temps d'exécution : " + timestamp(clock0));
globalTime += (System.nanoTime() - clock0);
count++;
return result;
}
public static String timestamp(long clock0) {
String result = null;
if (clock0 > 0) {
double elapsed = (System.nanoTime() - clock0) / 1e9;
String unit = "s";
if (elapsed < 1.0) {
elapsed *= 1000.0;
unit = "ms";
}
result = String.format("%.4g%s elapsed", elapsed, unit);
}
return result;
}
public static long timestamp() {
return System.nanoTime();
}
public static long globalTime;
public static Integer count;
public static void init() {
globalTime = 0;
count = 0;
}
public static Long getGlobalTime() {
return globalTime;
}
public static Integer getCount() {
return count;
}
}