From a789cfb851727db529c01a919a7231f2f05c82c9 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 9 Jul 2020 01:47:09 -0400 Subject: [PATCH] Added a benchmark function --- .../mattrixwv/ProjectEuler/Benchmark.java | 162 ++++++++++++++++++ .../java/mattrixwv/ProjectEuler/Driver.java | 110 ++---------- .../ProjectEuler/ProblemSelection.java | 87 ++++++++++ .../ProjectEuler/Problems/Problem.java | 3 + .../ProjectEuler/Problems/Problem32.java | 17 ++ 5 files changed, 286 insertions(+), 93 deletions(-) create mode 100644 src/main/java/mattrixwv/ProjectEuler/Benchmark.java create mode 100644 src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java create mode 100644 src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java diff --git a/src/main/java/mattrixwv/ProjectEuler/Benchmark.java b/src/main/java/mattrixwv/ProjectEuler/Benchmark.java new file mode 100644 index 0000000..4f4fed0 --- /dev/null +++ b/src/main/java/mattrixwv/ProjectEuler/Benchmark.java @@ -0,0 +1,162 @@ +package mattrixwv.ProjectEuler; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +import mattrixwv.Stopwatch; +import mattrixwv.ProjectEuler.Problems.Problem; + +public class Benchmark{ + private static final Scanner input = new Scanner(System.in); + private static enum BenchmarkOptions{runSpecific, runAllShort, runAll, exit, size}; + private static final ArrayList tooLong = new ArrayList(Arrays.asList(15)); + public static void benchmarkMenu(){ + BenchmarkOptions selection = BenchmarkOptions.size; + + printMenu(); + selection = getMenuSelection(); + + switch(selection){ + case runSpecific : runSpecific(); break; + case runAllShort : runAllShort(); break; + case runAll : runAll(); break; + case exit : break; + case size : break; + } + } + private static void printMenu(){ + 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("3. Run all problems"); + System.out.println("4. Exit the menu"); + System.out.println(); + } + private static BenchmarkOptions getMenuSelection(){ + int selection = input.nextInt(); + while(!isValidMenu(selection)){ + System.out.println("That is an invalid option!Press Enter to continue"); + printMenu(); + selection = input.nextInt(); + } + return getSelection(selection); + } + private static boolean isValidMenu(int selection){ + //Ordinal + 1 because enum starts at 0 + if((selection > 0) && (selection < (BenchmarkOptions.size.ordinal() + 1))){ + return true; + } + else{ + return false; + } + } + private static BenchmarkOptions getSelection(Integer selection){ + BenchmarkOptions sel = null; + + switch(selection){ + case 1 : sel = BenchmarkOptions.runSpecific; break; + case 2 : sel = BenchmarkOptions.runAllShort; break; + case 3 : sel = BenchmarkOptions.runAll; break; + case 4 : sel = BenchmarkOptions.exit; break; + default : sel = BenchmarkOptions.size; + } + return sel; + } + private static void runSpecific(){ + double totalTime = 0; + //Ask which problem the user wants to run + int problemNumber = ProblemSelection.getProblemNumber(); + //Ask how many times to run the problem + int timesToRun = getNumberOfTimesToRun(); + //Get the problem + Problem problem = ProblemSelection.getProblem(problemNumber); + //Run the problem the specific number of times + System.out.println(problemNumber + ". " + problem.getDescription()); + 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(); + } + //Calculate the average run time + totalTime /= timesToRun; + String timeResults = Stopwatch.getStr(totalTime); + //Print the results + System.out.println("\n" + problem.getResult()); + System.out.println("It took an average of " + timeResults + " to run this problem through " + timesToRun + "iterations\n\n"); + } + private static void runAllShort(){ + //Ask how many times to run the problem + int timesToRun = getNumberOfTimesToRun(); + //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){ + 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(tooLong.contains(problemNumber)){ + continue; + } + Problem problem = ProblemSelection.getProblem(problemNumber); + //Run each problem the specified number of times + System.out.println(problemNumber + ". " + problem.getDescription()); + System.out.print("Solving"); + 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 + System.out.println("\n\n" + problem.getDescription()); + System.out.println("It took an average of " + timeResults + " to run this problem through " + timesToRun + "iterations\n\n"); + } + } + private static void runAll(){ + //Ask how many times to run the problem + int timesToRun = getNumberOfTimesToRun(); + //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){ + int problemNumber = ProblemSelection.PROBLEM_NUMBERS.get(cnt); + double totalTime = 0; + Problem problem = ProblemSelection.getProblem(problemNumber); + //Run each problem the specified number of times + System.out.println(problemNumber + ". " + problem.getDescription()); + System.out.print("Solving"); + 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 + System.out.println("\n\n" + problem.getDescription()); + System.out.println("It took an average of " + timeResults + " to run this problem through " + timesToRun + "iterations\n\n"); + } + } + private static int getNumberOfTimesToRun(){ + int numOfTimesToRun = 1; + System.out.print("How many times do you want to run this problem? "); + numOfTimesToRun = input.nextInt(); + while(numOfTimesToRun < 1){ + System.out.println("That is an invalid number!"); + System.out.println("How many times do you want to run this problem? "); + numOfTimesToRun = input.nextInt(); + } + return numOfTimesToRun; + } +} \ No newline at end of file diff --git a/src/main/java/mattrixwv/ProjectEuler/Driver.java b/src/main/java/mattrixwv/ProjectEuler/Driver.java index 3717240..3eb6a69 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Driver.java +++ b/src/main/java/mattrixwv/ProjectEuler/Driver.java @@ -6,19 +6,11 @@ package mattrixwv.ProjectEuler; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Scanner; -import mattrixwv.ProjectEuler.Problems.*; - public class Driver{ - private static final ArrayList PROBLEM_NUMBERS = new ArrayList(Arrays.asList(0, 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, 67)); - private static enum SELECTIONS{SOLVE, DESCRIPTION, LIST, EXIT, SIZE}; + private static enum SELECTIONS{SOLVE, DESCRIPTION, LIST, EXIT, BENCHMARK, SIZE}; private static final Scanner input = new Scanner(System.in); public static void main(String[] args){ @@ -31,7 +23,8 @@ public class Driver{ switch(selection){ case SOLVE : solveMenu(); break; case DESCRIPTION : descriptionMenu(); break; - case LIST : listProblems(); break; + case LIST : ProblemSelection.listProblems(); break; + case BENCHMARK : Benchmark.benchmarkMenu(); break; case EXIT : break; case SIZE : default : printErrorMessage(); @@ -42,7 +35,8 @@ public class Driver{ System.out.println("1. Solve a problem"); System.out.println("2. Print a problem description"); System.out.println("3. List valid problem numbers"); - System.out.println("4. Exit"); + System.out.println("4. Benchmark"); + System.out.println("5. Exit"); System.out.println(); } private static SELECTIONS getMenuSelection(){ @@ -70,7 +64,8 @@ public class Driver{ case 1 : sel = SELECTIONS.SOLVE; break; case 2 : sel = SELECTIONS.DESCRIPTION; break; case 3 : sel = SELECTIONS.LIST; break; - case 4 : sel = SELECTIONS.EXIT; break; + case 4 : sel = SELECTIONS.BENCHMARK; break; + case 5 : sel = SELECTIONS.EXIT; break; default : sel = SELECTIONS.SIZE; } return sel; @@ -79,113 +74,42 @@ public class Driver{ System.out.println("That is an invalid selection!"); } private static void solveMenu(){ - Integer problemNumber = getProblemNumber(); + Integer problemNumber = ProblemSelection.getProblemNumber(); //This selection solves all problems in order if(problemNumber.equals(0)){ //Solve to every valid problem number, skipping over 0 - for(Integer problemLocation = 1;problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ + for(Integer problemLocation = 1;problemLocation < ProblemSelection.PROBLEM_NUMBERS.size();++problemLocation){ //Solve the problems - System.out.print(PROBLEM_NUMBERS.get(problemLocation).toString() + ". "); - solveProblem(PROBLEM_NUMBERS.get(problemLocation)); + System.out.print(ProblemSelection.PROBLEM_NUMBERS.get(problemLocation).toString() + ". "); + ProblemSelection.solveProblem(ProblemSelection.PROBLEM_NUMBERS.get(problemLocation)); } } //This is if a single problem number was chosen else{ //Solve the problem - solveProblem(problemNumber); + ProblemSelection.solveProblem(problemNumber); } } - private static void solveProblem(Integer problemNumber){ - //Get the problem - Problem problem = getProblem(problemNumber); - //Print the problem description - System.out.println(problem.getDescription()); - //Solve the problem - problem.solve(); - //Print the results - System.out.println(problem.getResult() + "\nIt took " + problem.getTime() + " to solve this problem.\n\n"); - } - private static Problem getProblem(Integer problemNumber){ - Problem problem = null; - switch(problemNumber){ - case 1 : problem = new Problem1(); break; - case 2 : problem = new Problem2(); break; - case 3 : problem = new Problem3(); break; - case 4 : problem = new Problem4(); break; - case 5 : problem = new Problem5(); break; - case 6 : problem = new Problem6(); break; - case 7 : problem = new Problem7(); break; - case 8 : problem = new Problem8(); break; - case 9 : problem = new Problem9(); break; - case 10 : problem = new Problem10(); break; - case 11 : problem = new Problem11(); break; - case 12 : problem = new Problem12(); break; - case 13 : problem = new Problem13(); break; - case 14 : problem = new Problem14(); break; - case 15 : problem = new Problem15(); break; - case 16 : problem = new Problem16(); break; - case 17 : problem = new Problem17(); break; - case 18 : problem = new Problem18(); break; - case 19 : problem = new Problem19(); break; - case 20 : problem = new Problem20(); break; - case 21 : problem = new Problem21(); break; - case 22 : problem = new Problem22(); break; - case 23 : problem = new Problem23(); break; - case 24 : problem = new Problem24(); break; - case 25 : problem = new Problem25(); break; - case 26 : problem = new Problem26(); break; - case 27 : problem = new Problem27(); break; - case 28 : problem = new Problem28(); break; - case 29 : problem = new Problem29(); break; - case 30 : problem = new Problem30(); break; - case 31 : problem = new Problem31(); break; - case 67 : problem = new Problem67(); break; - } - return problem; - } private static void descriptionMenu(){ //Give some extra space to print the description System.out.println("\n"); //Get the problem number - Integer problemNumber = getProblemNumber(); + Integer problemNumber = ProblemSelection.getProblemNumber(); //If the problem number is 0 print out all the descriptions if(problemNumber.equals(0)){ //Print description for every valid problem number, skipping over 0 - for(Integer problemLocation = 1;problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ + for(Integer problemLocation = 1;problemLocation < ProblemSelection.PROBLEM_NUMBERS.size();++problemLocation){ //Print the problem's description - System.out.print(PROBLEM_NUMBERS.get(problemLocation) + ". "); - printDescription(PROBLEM_NUMBERS.get(problemLocation)); + System.out.print(ProblemSelection.PROBLEM_NUMBERS.get(problemLocation) + ". "); + ProblemSelection.printDescription(ProblemSelection.PROBLEM_NUMBERS.get(problemLocation)); System.out.println(); } } //Otherwise print out a single problem's description else{ - printDescription(problemNumber); + ProblemSelection.printDescription(problemNumber); } } - private static void printDescription(Integer problemNumber){ - //Get the problem - Problem problem = getProblem(problemNumber); - //Print the problem's description - System.out.println(problem.getDescription()); - } - private static Integer getProblemNumber(){ - Integer problemNumber = 0; - System.out.print("Enter a problem number: "); - problemNumber = input.nextInt(); - while(!PROBLEM_NUMBERS.contains(problemNumber)){ - System.out.print("That is an invalid problem number!\nEnter a problem number: "); - problemNumber = input.nextInt(); - } - return problemNumber; - } - private static void listProblems(){ - System.out.print(PROBLEM_NUMBERS.get(1)); - for(Integer problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){ - System.out.print(", " + PROBLEM_NUMBERS.get(problemNumber).toString()); - } - System.out.println(); - } } diff --git a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java new file mode 100644 index 0000000..04bb2a7 --- /dev/null +++ b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java @@ -0,0 +1,87 @@ +package mattrixwv.ProjectEuler; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +import mattrixwv.ProjectEuler.Problems.*; + +public class ProblemSelection{ + private static final Scanner input = new Scanner(System.in); + public static final ArrayList PROBLEM_NUMBERS = new ArrayList(Arrays.asList(0, 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, 67)); + + public static Problem getProblem(Integer problemNumber){ + Problem problem = null; + switch(problemNumber){ + case 1 : problem = new Problem1(); break; + case 2 : problem = new Problem2(); break; + case 3 : problem = new Problem3(); break; + case 4 : problem = new Problem4(); break; + case 5 : problem = new Problem5(); break; + case 6 : problem = new Problem6(); break; + case 7 : problem = new Problem7(); break; + case 8 : problem = new Problem8(); break; + case 9 : problem = new Problem9(); break; + case 10 : problem = new Problem10(); break; + case 11 : problem = new Problem11(); break; + case 12 : problem = new Problem12(); break; + case 13 : problem = new Problem13(); break; + case 14 : problem = new Problem14(); break; + case 15 : problem = new Problem15(); break; + case 16 : problem = new Problem16(); break; + case 17 : problem = new Problem17(); break; + case 18 : problem = new Problem18(); break; + case 19 : problem = new Problem19(); break; + case 20 : problem = new Problem20(); break; + case 21 : problem = new Problem21(); break; + case 22 : problem = new Problem22(); break; + case 23 : problem = new Problem23(); break; + case 24 : problem = new Problem24(); break; + case 25 : problem = new Problem25(); break; + case 26 : problem = new Problem26(); break; + case 27 : problem = new Problem27(); break; + case 28 : problem = new Problem28(); break; + case 29 : problem = new Problem29(); break; + case 30 : problem = new Problem30(); break; + case 31 : problem = new Problem31(); break; + case 67 : problem = new Problem67(); break; + } + return problem; + } + public static void printDescription(Integer problemNumber){ + //Get the problem + Problem problem = getProblem(problemNumber); + //Print the problem's description + System.out.println(problem.getDescription()); + } + public static void solveProblem(Integer problemNumber){ + //Get the problem + Problem problem = getProblem(problemNumber); + //Print the problem description + System.out.println(problem.getDescription()); + //Solve the problem + problem.solve(); + //Print the results + System.out.println(problem.getResult() + "\nIt took " + problem.getTime() + " to solve this problem.\n\n"); + } + public static Integer getProblemNumber(){ + Integer problemNumber = 0; + System.out.print("Enter a problem number: "); + problemNumber = input.nextInt(); + while(!PROBLEM_NUMBERS.contains(problemNumber)){ + System.out.print("That is an invalid problem number!\nEnter a problem number: "); + problemNumber = input.nextInt(); + } + return problemNumber; + } + public static void listProblems(){ + System.out.print(PROBLEM_NUMBERS.get(1)); + for(Integer problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){ + System.out.print(", " + PROBLEM_NUMBERS.get(problemNumber).toString()); + } + System.out.println(); + } +} diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem.java index 18be03f..3b04d7c 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem.java @@ -40,5 +40,8 @@ public abstract class Problem{ public String getResult(){ return result; } + public Stopwatch getTimer(){ + return timer; + } public abstract void solve(); } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java new file mode 100644 index 0000000..aa5e063 --- /dev/null +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java @@ -0,0 +1,17 @@ +package mattrixwv.ProjectEuler.Problems; + +public class Problem32 extends Problem{ + Problem32(){ + super(""); + } + + @Override + public void solve(){ + //1X1-4 + //2X1-3 + } +} + +/* Results: + +*/