use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise25_DoublingTestRandomGrids method doExperiments.
private void doExperiments(int numberOfExperiments, int unionFindType) {
List<Experiment> experiments = new ArrayList<>();
int numberOfSites = 512;
// Previous time
UF initialUnionFind = generateUnionFind(numberOfSites / 2, unionFindType);
Stopwatch initialTimer = new Stopwatch();
randomGridsConnection(numberOfSites / 2, initialUnionFind);
double previousRunningTime = initialTimer.elapsedTime();
for (int i = 0; i < numberOfExperiments; i++) {
UF unionFind = generateUnionFind(numberOfSites, unionFindType);
Stopwatch timer = new Stopwatch();
int pairsGenerated = randomGridsConnection(numberOfSites, unionFind);
double runningTime = timer.elapsedTime();
Experiment experiment = new Experiment(numberOfSites, pairsGenerated, runningTime / previousRunningTime);
experiments.add(experiment);
previousRunningTime = runningTime;
numberOfSites *= 2;
}
printResults(experiments);
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise30_GeometricIncrements method time.
public static double time(Comparable[] array, Integer[] incrementSequence) {
Stopwatch timer = new Stopwatch();
shellsort(array, incrementSequence);
return timer.elapsedTime();
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise31_DoublingTest method doExperiments.
private static void doExperiments(SortTypes sortType, int arrayLength, int numberOfExperiments) {
// Previous time
Comparable[] array = generateRandomArray(arrayLength);
Stopwatch initialTimer = new Stopwatch();
sortArray(array, sortType);
double previousRunningTime = initialTimer.elapsedTime();
for (int i = 0; i < numberOfExperiments; i++) {
arrayLength *= 2;
array = generateRandomArray(arrayLength);
Stopwatch timer = new Stopwatch();
sortArray(array, sortType);
double runningTime = timer.elapsedTime();
/**
* Predicted seconds
* Selection and Insertion sort - N ^ 2
* Expected doubling ratio = 2 ^ 2 = 4
*
* Shell sort - N ^ 3/2
* Expected doubling ratio = 2 ^ 3/2 = 2.82
*/
double doublingRatio = 0;
if (sortType == SortTypes.SELECTION || sortType == SortTypes.INSERTION) {
doublingRatio = 4;
} else if (sortType == SortTypes.SHELL) {
doublingRatio = 2.82;
}
double predictedSeconds = previousRunningTime * doublingRatio;
double ratio = runningTime / previousRunningTime;
printExperiment(arrayLength, predictedSeconds, runningTime, ratio);
previousRunningTime = runningTime;
}
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise41_RunningTimes method timeTrial.
private double timeTrial(int n, int sumMethod) {
int max = 1000000;
int[] numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = StdRandom.uniform(-max, max);
}
Stopwatch timer = new Stopwatch();
switch(sumMethod) {
case 0:
twoSum(numbers);
break;
case 1:
twoSumFast(numbers);
break;
case 2:
threeSum(numbers);
break;
case 3:
threeSumFast(numbers);
break;
}
return timer.elapsedTime();
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise43_ResizingArrayXLinkedList method timeTrial.
private double timeTrial(int n, Stack<Integer> stack) {
int max = 1000000;
int[] numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = StdRandom.uniform(-max, max);
}
Stopwatch timer = new Stopwatch();
for (int number : numbers) {
stack.push(number);
}
while (!stack.isEmpty()) {
stack.pop();
}
return timer.elapsedTime();
}
Aggregations