-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexity.java
More file actions
114 lines (87 loc) · 2.66 KB
/
Copy pathComplexity.java
File metadata and controls
114 lines (87 loc) · 2.66 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class Complexity {
double[] input;
public Complexity(double[] input) {
this.input = input;
}
/**
* Ritorna la granularità del sistema
*/
public long getGranularity() {
long tStart = System.currentTimeMillis();
long tEnd = System.currentTimeMillis();
while(tStart == tEnd){
tEnd = System.currentTimeMillis();
}
return tEnd - tStart;
}
/*
* tMin = 20 * granularità.
*/
public int getNumberOfTest(long tMin) {
long t0 = 0;
long t1 = 0;
int repsCount = 1;
long currentExecutionTime = t1 - t0;
while(currentExecutionTime <= tMin) {
repsCount = repsCount * 2;
t0 = System.currentTimeMillis();
for(int i = 0; i < repsCount; i++) {
Main.execute(input);
}
t1 = System.currentTimeMillis();
currentExecutionTime = t1 - t0;
}
int maxRepsCount = repsCount;
int minRepsCount = repsCount / 2;
int wrongCicles = 5;
while(maxRepsCount - minRepsCount >= wrongCicles) {
int middleReps = (maxRepsCount+minRepsCount) / 2;
t0 = System.currentTimeMillis();
for(int i = 0; i < middleReps; i++) {
Main.execute(input);
}
t1 = System.currentTimeMillis();
if(t1 - t0 <= tMin){
minRepsCount = middleReps;
} else {
maxRepsCount = repsCount;
}
}
return maxRepsCount;
}
public long timeCalculator(int repsCount) {
long t0 = System.currentTimeMillis();
for(int i = 0; i < repsCount; i++) {
Main.execute(input);
}
long t1 = System.currentTimeMillis();
long tTot = t1 - t0;
long tSing = tTot / repsCount;
return tSing;
}
/*
* rip = ripetizioni, c = 5 o 10, za = fissato,
*/
public double timeMisurator(double za, int rip, int c) {
double mediumValue, sqrtResult, delta;
double deltaMaiusc = 0; // 1/5 del tempo medio;
double stats = 0;
double numberOfCicles = 0;
double sumOfAllExecutionsTimes = 0;
do {
//c = 5, ciclo eseguito per 5 volte
//in tutto il programma viene eseguito c*rip ogni volta
for(int i = 0; i < c; i++){
long timeForRipTimes = timeCalculator(rip); //esegue il programma per rip volte e calcola il tempo medio
sumOfAllExecutionsTimes = sumOfAllExecutionsTimes + timeForRipTimes; //somma al tempo totale il tempo dell'esecuzione corrente
stats = stats + (timeForRipTimes*timeForRipTimes);
}
numberOfCicles += c;
mediumValue = sumOfAllExecutionsTimes / numberOfCicles; //tempo medio corrente
sqrtResult = Math.sqrt((stats / numberOfCicles) - mediumValue*mediumValue);
delta = (1 / (Math.sqrt(numberOfCicles))) * za * sqrtResult;
deltaMaiusc = mediumValue / 5; //DELTA è settato ad 1/5 del tempo medio
} while(delta >= deltaMaiusc);
return mediumValue;
}
}