use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise37_AutoboxingPerformance method timeTrial.
private static double timeTrial(long trials, boolean useGenericStack) {
Stopwatch timer = new Stopwatch();
// Trials only do a push and a pop operation
for (int i = 0; i < trials; i++) {
if (!useGenericStack) {
fixedCapacityInteger.push(i);
fixedCapacityInteger.pop();
} else {
fixedCapacityGeneric.push(i);
fixedCapacityGeneric.pop();
}
}
return timer.elapsedTime();
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise38_Naive3Sum method timeTrial.
private static double timeTrial(int n, boolean useEvenNaiverImplementation) {
// Time ThreeSum.count() for n random 6-digit ints.
int max = 1000000;
int[] values = new int[n];
for (int i = 0; i < n; i++) {
values[i] = StdRandom.uniform(-max, max);
}
Stopwatch timer = new Stopwatch();
if (useEvenNaiverImplementation) {
evenNaiverThreeSumCount(values);
} else {
threeSumCount(values);
}
return timer.elapsedTime();
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise42_ProblemSizes 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 Exercise32_ExerciseDriver method doExperiment.
private static void doExperiment(int numberOfExperiments, int initialArraySize) {
StdOut.printf("%28s %13s %12s\n", "Input Type | ", "Array Size | ", "Running Time");
String[] inputType = { ORDERED_INPUT, REVERSE_ORDERED_INPUT, SAME_KEYS_INPUT, KEYS_WITH_ONLY_2_VALUES_INPUT };
for (int input = 0; input < 4; input++) {
Map<Integer, Comparable[]> allInputArrays = generateAllArrays(inputType[input], numberOfExperiments, initialArraySize);
for (int i = 0; i < numberOfExperiments; i++) {
Comparable[] array = allInputArrays.get(i);
Stopwatch timer = new Stopwatch();
testInput(array, false);
double runningTime = timer.elapsedTime();
printResults(inputType[input], array.length, runningTime);
}
}
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise34_ZipfsLaw method doExperiment.
private void doExperiment(Map<Integer, Comparable[]> allInputArrays) {
StdOut.printf("%10s %25s %25s\n", "Array Size | ", "Move-to-Front Time | ", "Optimal Arrangement Time");
for (int i = 0; i < allInputArrays.size(); i++) {
BinarySearchSymbolTable<Integer, Integer> binarySearchSymbolTable = new BinarySearchSymbolTable<>();
Exercise22_SelfOrganizingSearch selfOrganizingSearch = new Exercise22_SelfOrganizingSearch();
Exercise22_SelfOrganizingSearch.ArraySTSelfOrganizing<Integer, Integer> arraySTSelfOrganizing = selfOrganizingSearch.new ArraySTSelfOrganizing<>(2);
Comparable[] array = allInputArrays.get(i);
double[] probabilityDistribution = new double[array.length];
for (int p = 0; p < array.length; p++) {
double lnN = Math.log(array.length);
probabilityDistribution[p] = 1.0 / ((p + 1) * lnN);
}
// Self-Organizing Search Symbol Table
Stopwatch selfOrganizingSearchTimer = new Stopwatch();
for (Comparable key : array) {
int randomValue = StdRandom.uniform(0, 2);
arraySTSelfOrganizing.put((int) key, randomValue);
}
for (int search = 0; search < 10 * array.length; search++) {
double keyProbabilityToSearch = StdRandom.uniform();
for (int p = 0; p < probabilityDistribution.length; p++) {
keyProbabilityToSearch -= probabilityDistribution[p];
if (keyProbabilityToSearch <= 0) {
// Search hit
arraySTSelfOrganizing.get((int) array[p]);
break;
}
}
}
double selfOrganizingSearchRunningTime = selfOrganizingSearchTimer.elapsedTime();
// Binary Search Symbol Table
Stopwatch binarySearchTimer = new Stopwatch();
for (Comparable key : array) {
int randomValue = StdRandom.uniform(0, 2);
binarySearchSymbolTable.put((int) key, randomValue);
}
for (int search = 0; search < 10 * array.length; search++) {
double keyProbabilityToSearch = StdRandom.uniform();
for (int p = 0; p < probabilityDistribution.length; p++) {
keyProbabilityToSearch -= probabilityDistribution[p];
if (keyProbabilityToSearch <= 0) {
// Search hit
binarySearchSymbolTable.get((int) array[p]);
break;
}
}
}
double binarySearchRunningTime = binarySearchTimer.elapsedTime();
printResults(array.length, selfOrganizingSearchRunningTime, binarySearchRunningTime);
}
}
Aggregations