mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Added some functions to reduce the amount of copied code
This commit is contained in:
@@ -1,5 +1,27 @@
|
|||||||
|
//ProjectEuler/ProjectEulerJava/src/man/java/mattrixwv/ProjectEuler/Benchmark.java
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 07-08-20
|
||||||
|
//Modified: 07-09-20
|
||||||
|
//This is the driver function for the Java version of the ProjectEuler project
|
||||||
|
/*
|
||||||
|
Copyright (C) 2020 Matthew Ellison
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
package mattrixwv.ProjectEuler;
|
package mattrixwv.ProjectEuler;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
@@ -7,10 +29,12 @@ import java.util.Scanner;
|
|||||||
import mattrixwv.Stopwatch;
|
import mattrixwv.Stopwatch;
|
||||||
import mattrixwv.ProjectEuler.Problems.Problem;
|
import mattrixwv.ProjectEuler.Problems.Problem;
|
||||||
|
|
||||||
|
|
||||||
public class Benchmark{
|
public class Benchmark{
|
||||||
private static final Scanner input = new Scanner(System.in);
|
private static final Scanner input = new Scanner(System.in);
|
||||||
private static enum BenchmarkOptions{runSpecific, runAllShort, runAll, exit, size};
|
private static enum BenchmarkOptions{runSpecific, runAllShort, runAll, exit, size};
|
||||||
private static final ArrayList<Integer> tooLong = new ArrayList<Integer>(Arrays.asList(15));
|
private static final ArrayList<Integer> tooLong = new ArrayList<Integer>(Arrays.asList(15, 23, 24, 27));
|
||||||
|
//The driver function for the benchmark selection
|
||||||
public static void benchmarkMenu(){
|
public static void benchmarkMenu(){
|
||||||
BenchmarkOptions selection = BenchmarkOptions.size;
|
BenchmarkOptions selection = BenchmarkOptions.size;
|
||||||
|
|
||||||
@@ -25,6 +49,7 @@ public class Benchmark{
|
|||||||
case size : break;
|
case size : break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Print the benchmark menu
|
||||||
private static void printMenu(){
|
private static void printMenu(){
|
||||||
System.out.println("1. Run a specific problem");
|
System.out.println("1. Run a specific problem");
|
||||||
System.out.println("2. Run all problems that have a reasonably short run time");
|
System.out.println("2. Run all problems that have a reasonably short run time");
|
||||||
@@ -32,15 +57,17 @@ public class Benchmark{
|
|||||||
System.out.println("4. Exit the menu");
|
System.out.println("4. Exit the menu");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
//Returns a valid menu option
|
||||||
private static BenchmarkOptions getMenuSelection(){
|
private static BenchmarkOptions getMenuSelection(){
|
||||||
int selection = input.nextInt();
|
int selection = input.nextInt();
|
||||||
while(!isValidMenu(selection)){
|
while(!isValidMenu(selection)){
|
||||||
System.out.println("That is an invalid option!Press Enter to continue");
|
System.out.println("That is an invalid option!\nPress Enter to continue");
|
||||||
printMenu();
|
printMenu();
|
||||||
selection = input.nextInt();
|
selection = input.nextInt();
|
||||||
}
|
}
|
||||||
return getSelection(selection);
|
return getSelection(selection);
|
||||||
}
|
}
|
||||||
|
//Determines if a value is a valid menu option. Helper for getBechmarkMenuSelection
|
||||||
private static boolean isValidMenu(int selection){
|
private static boolean isValidMenu(int selection){
|
||||||
//Ordinal + 1 because enum starts at 0
|
//Ordinal + 1 because enum starts at 0
|
||||||
if((selection > 0) && (selection < (BenchmarkOptions.size.ordinal() + 1))){
|
if((selection > 0) && (selection < (BenchmarkOptions.size.ordinal() + 1))){
|
||||||
@@ -50,6 +77,7 @@ public class Benchmark{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//A helper function for getMenuSelection that turns an integer to a BenchmarkOptions
|
||||||
private static BenchmarkOptions getSelection(Integer selection){
|
private static BenchmarkOptions getSelection(Integer selection){
|
||||||
BenchmarkOptions sel = null;
|
BenchmarkOptions sel = null;
|
||||||
|
|
||||||
@@ -62,92 +90,69 @@ public class Benchmark{
|
|||||||
}
|
}
|
||||||
return sel;
|
return sel;
|
||||||
}
|
}
|
||||||
|
//Determines which problem user wants to run and runs it
|
||||||
private static void runSpecific(){
|
private static void runSpecific(){
|
||||||
double totalTime = 0;
|
|
||||||
//Ask which problem the user wants to run
|
//Ask which problem the user wants to run
|
||||||
int problemNumber = ProblemSelection.getProblemNumber();
|
int problemNumber = ProblemSelection.getProblemNumber();
|
||||||
//Ask how many times to run the problem
|
//Ask how many times to run the problem
|
||||||
int timesToRun = getNumberOfTimesToRun();
|
int timesToRun = getNumberOfTimesToRun();
|
||||||
//Get the problem
|
|
||||||
|
//Get the problem and print its description
|
||||||
Problem problem = ProblemSelection.getProblem(problemNumber);
|
Problem problem = ProblemSelection.getProblem(problemNumber);
|
||||||
//Run the problem the specific number of times
|
|
||||||
System.out.println(problemNumber + ". " + problem.getDescription());
|
System.out.println(problemNumber + ". " + problem.getDescription());
|
||||||
System.out.print("Solving");
|
|
||||||
for(int cnt = 0;cnt < timesToRun;++cnt){
|
//Run the problem the specific number of times
|
||||||
//Reset the data so you are actually counting the run time a second time
|
double totalTime = runProblem(problem, timesToRun);
|
||||||
System.out.print('.');
|
|
||||||
//Solve the problem
|
|
||||||
problem.solve();
|
|
||||||
//Get the time data
|
|
||||||
totalTime += problem.getTimer().getNano();
|
|
||||||
}
|
|
||||||
//Calculate the average run time
|
|
||||||
totalTime /= timesToRun;
|
|
||||||
String timeResults = Stopwatch.getStr(totalTime);
|
|
||||||
//Print the results
|
//Print the results
|
||||||
System.out.println("\n" + problem.getResult());
|
System.out.println(getBenchmarkResults(problem, totalTime, timesToRun));
|
||||||
System.out.println("It took an average of " + timeResults + " to run this problem through " + timesToRun + " iterations\n\n");
|
|
||||||
}
|
}
|
||||||
|
//Runs all problems except a few that are specified because of run length
|
||||||
private static void runAllShort(){
|
private static void runAllShort(){
|
||||||
//Ask how many times to run the problem
|
//Ask how many times to run the problem
|
||||||
int timesToRun = getNumberOfTimesToRun();
|
int timesToRun = getNumberOfTimesToRun();
|
||||||
|
|
||||||
//Run through all valid problem numbers, skipping a few that are in the tooLong list
|
//Run through all valid problem numbers, skipping a few that are in the tooLong list
|
||||||
for(int cnt = 1;cnt < ProblemSelection.PROBLEM_NUMBERS.size();++cnt){
|
for(int cnt = 1;cnt < ProblemSelection.PROBLEM_NUMBERS.size();++cnt){
|
||||||
int problemNumber = ProblemSelection.PROBLEM_NUMBERS.get(cnt);
|
int problemNumber = ProblemSelection.PROBLEM_NUMBERS.get(cnt);
|
||||||
double totalTime = 0;
|
|
||||||
//If the problem number is contained in the list of problems that take too long skip it
|
//If the problem number is contained in the list of problems that take too long skip it
|
||||||
if(tooLong.contains(problemNumber)){
|
if(tooLong.contains(problemNumber)){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Get the problem and print its description
|
||||||
Problem problem = ProblemSelection.getProblem(problemNumber);
|
Problem problem = ProblemSelection.getProblem(problemNumber);
|
||||||
//Run each problem the specified number of times
|
|
||||||
System.out.println(problemNumber + ". " + problem.getDescription());
|
System.out.println(problemNumber + ". " + problem.getDescription());
|
||||||
System.out.print("Solving");
|
|
||||||
for(int cnt2 = 0;cnt2 < timesToRun;++cnt2){
|
//Run each problem the specified number of times
|
||||||
//Get the problem
|
double totalTime = runProblem(problem, timesToRun);
|
||||||
problem = ProblemSelection.getProblem(problemNumber);
|
|
||||||
System.out.print('.');
|
|
||||||
//Solve the problem
|
|
||||||
problem.solve();
|
|
||||||
//Get the time data
|
|
||||||
totalTime += problem.getTimer().getNano();
|
|
||||||
}
|
|
||||||
//Calculate the averate run time of the problem
|
|
||||||
totalTime /= timesToRun;
|
|
||||||
String timeResults = Stopwatch.getStr(totalTime);
|
|
||||||
//Print the results
|
//Print the results
|
||||||
System.out.println("\n\n" + problem.getResult());
|
System.out.println(getBenchmarkResults(problem, totalTime, timesToRun));
|
||||||
System.out.println("It took an average of " + timeResults + " to run this problem through " + timesToRun + " iterations\n\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Runs all problems
|
||||||
private static void runAll(){
|
private static void runAll(){
|
||||||
//Ask how many times to run the problem
|
//Ask how many times to run the problem
|
||||||
int timesToRun = getNumberOfTimesToRun();
|
int timesToRun = getNumberOfTimesToRun();
|
||||||
|
|
||||||
//Run through all valid problem numbers, skipping a few that are in the tooLong list
|
//Run through all valid problem numbers, skipping a few that are in the tooLong list
|
||||||
for(int cnt = 1;cnt < ProblemSelection.PROBLEM_NUMBERS.size();++cnt){
|
for(int cnt = 1;cnt < ProblemSelection.PROBLEM_NUMBERS.size();++cnt){
|
||||||
int problemNumber = ProblemSelection.PROBLEM_NUMBERS.get(cnt);
|
int problemNumber = ProblemSelection.PROBLEM_NUMBERS.get(cnt);
|
||||||
double totalTime = 0;
|
|
||||||
|
//Get the problem
|
||||||
Problem problem = ProblemSelection.getProblem(problemNumber);
|
Problem problem = ProblemSelection.getProblem(problemNumber);
|
||||||
|
|
||||||
//Run each problem the specified number of times
|
//Run each problem the specified number of times
|
||||||
System.out.println(problemNumber + ". " + problem.getDescription());
|
System.out.println(problemNumber + ". " + problem.getDescription());
|
||||||
System.out.print("Solving");
|
double totalTime = runProblem(problem, timesToRun);
|
||||||
for(int cnt2 = 0;cnt2 < timesToRun;++cnt2){
|
|
||||||
//Get the problem
|
|
||||||
problem = ProblemSelection.getProblem(problemNumber);
|
|
||||||
System.out.print('.');
|
|
||||||
//Solve the problem
|
|
||||||
problem.solve();
|
|
||||||
//Get the time data
|
|
||||||
totalTime += problem.getTimer().getNano();
|
|
||||||
}
|
|
||||||
//Calculate the averate run time of the problem
|
|
||||||
totalTime /= timesToRun;
|
|
||||||
String timeResults = Stopwatch.getStr(totalTime);
|
|
||||||
//Print the results
|
//Print the results
|
||||||
System.out.println("\n\n" + problem.getResult());
|
System.out.println(getBenchmarkResults(problem, totalTime, timesToRun));
|
||||||
System.out.println("It took an average of " + timeResults + " to run this problem through " + timesToRun + " iterations\n\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Asks how many times a problem is supposed to run and returns the value
|
||||||
private static int getNumberOfTimesToRun(){
|
private static int getNumberOfTimesToRun(){
|
||||||
int numOfTimesToRun = 1;
|
int numOfTimesToRun = 1;
|
||||||
System.out.print("How many times do you want to run this problem? ");
|
System.out.print("How many times do you want to run this problem? ");
|
||||||
@@ -159,4 +164,28 @@ public class Benchmark{
|
|||||||
}
|
}
|
||||||
return numOfTimesToRun;
|
return numOfTimesToRun;
|
||||||
}
|
}
|
||||||
|
//Runs the problem the given number of times
|
||||||
|
private static double runProblem(Problem problem, int timesToRun){
|
||||||
|
double totalTime = 0;
|
||||||
|
System.out.print("Solving");
|
||||||
|
for(int cnt = 0;cnt < timesToRun;++cnt){
|
||||||
|
//Reset the data so you are actually counting the run time a second time
|
||||||
|
System.out.print('.');
|
||||||
|
//Solve the problem
|
||||||
|
problem.solve();
|
||||||
|
//Get the time data
|
||||||
|
totalTime += problem.getTimer().getNano();
|
||||||
|
}
|
||||||
|
return totalTime;
|
||||||
|
}
|
||||||
|
//Prints the benchmark results of a problem
|
||||||
|
private static String getBenchmarkResults(Problem problem, double totalTime, int timesRun){
|
||||||
|
//Calculate the averate run time of the problem
|
||||||
|
totalTime /= timesRun;
|
||||||
|
String timeResults = Stopwatch.getStr(totalTime);
|
||||||
|
|
||||||
|
//Tally the results
|
||||||
|
String results = "\n\n" + problem.getResult() + "\nIt took an average of " + timeResults + " to run this problem through " + timesRun + " iterations\n\n";
|
||||||
|
return results;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user