Refactored to make benchmark part of the main program

This commit is contained in:
2020-07-09 00:00:33 -04:00
parent 89bb42d4d9
commit 6f55685f48
5 changed files with 73 additions and 68 deletions

View File

@@ -5,9 +5,43 @@
//This is a header file with a few functions to help select and run problems
#ifndef PROBLEMSELECTION_HPP
#define PROBLEMSELECTION_HPP
#include <iostream>
#include "Problems.hpp"
#include "Algorithms.hpp"
#include "Headers/Problem1.hpp"
#include "Headers/Problem2.hpp"
#include "Headers/Problem3.hpp"
#include "Headers/Problem4.hpp"
#include "Headers/Problem5.hpp"
#include "Headers/Problem6.hpp"
#include "Headers/Problem7.hpp"
#include "Headers/Problem8.hpp"
#include "Headers/Problem9.hpp"
#include "Headers/Problem10.hpp"
#include "Headers/Problem11.hpp"
#include "Headers/Problem12.hpp"
#include "Headers/Problem13.hpp"
#include "Headers/Problem14.hpp"
#include "Headers/Problem15.hpp"
#include "Headers/Problem16.hpp"
#include "Headers/Problem17.hpp"
#include "Headers/Problem18.hpp"
#include "Headers/Problem19.hpp"
#include "Headers/Problem20.hpp"
#include "Headers/Problem21.hpp"
#include "Headers/Problem22.hpp"
#include "Headers/Problem23.hpp"
#include "Headers/Problem24.hpp"
#include "Headers/Problem25.hpp"
#include "Headers/Problem26.hpp"
#include "Headers/Problem27.hpp"
#include "Headers/Problem28.hpp"
#include "Headers/Problem29.hpp"
#include "Headers/Problem30.hpp"
#include "Headers/Problem31.hpp"
#include "Headers/Problem67.hpp"
//Setup the problem numbers
@@ -92,4 +126,6 @@ void listProblems(){
std::cout << ", " << PROBLEM_NUMBERS[problemNumber];
}
std::cout << std::endl;
}
}
#endif //PROBLEMSELECTION_HPP

View File

@@ -1,32 +0,0 @@
#include "Headers/Problem1.hpp"
#include "Headers/Problem2.hpp"
#include "Headers/Problem3.hpp"
#include "Headers/Problem4.hpp"
#include "Headers/Problem5.hpp"
#include "Headers/Problem6.hpp"
#include "Headers/Problem7.hpp"
#include "Headers/Problem8.hpp"
#include "Headers/Problem9.hpp"
#include "Headers/Problem10.hpp"
#include "Headers/Problem11.hpp"
#include "Headers/Problem12.hpp"
#include "Headers/Problem13.hpp"
#include "Headers/Problem14.hpp"
#include "Headers/Problem15.hpp"
#include "Headers/Problem16.hpp"
#include "Headers/Problem17.hpp"
#include "Headers/Problem18.hpp"
#include "Headers/Problem19.hpp"
#include "Headers/Problem20.hpp"
#include "Headers/Problem21.hpp"
#include "Headers/Problem22.hpp"
#include "Headers/Problem23.hpp"
#include "Headers/Problem24.hpp"
#include "Headers/Problem25.hpp"
#include "Headers/Problem26.hpp"
#include "Headers/Problem27.hpp"
#include "Headers/Problem28.hpp"
#include "Headers/Problem29.hpp"
#include "Headers/Problem30.hpp"
#include "Headers/Problem31.hpp"
#include "Headers/Problem67.hpp"

View File

@@ -1,65 +1,66 @@
//ProjectEulerCPP/benchmark.cpp
//ProjectEulerCPP/benchmark.hpp
//Mattrixwv
// Created: 07-08-20
//Modified: 07-08-20
//This is a program that runs a problem several times to get the average time it takes to run
#ifndef BENCHMARK_HPP
#define BENCHMARK_HPP
#include <iostream>
#include <vector>
#include "ProblemSelection.hpp"
void printMenu(); //Prints the menu to the screen
int getMenuSelection(); //Returns a valid menu option
bool isValidMenu(int selection); //Determines if a value is a valid menu option. Helper for getMenuSelection
void printBenchmarkMenu(); //Prints the menu to the screen
int getBenchmarkMenuSelection(); //Returns a valid menu option
bool isValidBenchmarkMenu(int selection); //Determines if a value is a valid menu option. Helper for getBenchmarkMenuSelection
void runSpecific(); //Determines which problem user wants to run and runs it
void runAllShort(); //Runs all problems except a few that are specified because of run length
void runAll(); //Runs all problems
unsigned int getNumberOfTimesToRun(); //Asks how many times a problem is supposed to run and returns the value
enum MENU_OPTIONS {RUN_SPECIFIC = 1, RUN_ALL_SHORT, RUN_ALL, EXIT, SIZE};
enum BENCHMARK_OPTIONS {RUN_SPECIFIC = 1, RUN_ALL_SHORT, RUN_ALL, BENCHMARK_EXIT, BENCHMARK_SIZE};
std::vector<unsigned int> tooLong = {15};
int main(){
void benchmarkMenu(){
int selection = 0;
do{
printMenu();
selection = getMenuSelection();
switch(selection){
case RUN_SPECIFIC: runSpecific(); break;
case RUN_ALL_SHORT: runAllShort(); break;
case RUN_ALL: runAll(); break;
case EXIT: break;
}
}while(selection != EXIT);
return 0;
printBenchmarkMenu();
selection = getBenchmarkMenuSelection();
switch(selection){
case BENCHMARK_OPTIONS::RUN_SPECIFIC: runSpecific(); break;
case BENCHMARK_OPTIONS::RUN_ALL_SHORT: runAllShort(); break;
case BENCHMARK_OPTIONS::RUN_ALL: runAll(); break;
case BENCHMARK_OPTIONS::BENCHMARK_EXIT: break;
}
}
void printMenu(){
void printBenchmarkMenu(){
std::cout << "1. Run a specific problem\n"
<< "2. Run all problems that have a reasonably short run time\n"
<< "3. Run all problems\n"
<< "4. Exit the program" << std::endl;
}
int getMenuSelection(){
int getBenchmarkMenuSelection(){
int selection = 0;
std::cin >> selection;
while(std::cin.fail() || !isValidMenu(selection)){
while(std::cin.fail() || !isValidBenchmarkMenu(selection)){
std::cout << "That is an invalid option!\nPress Enter to continue" << std::endl;
std::cin.clear();
std::cin.get();
printMenu();
printBenchmarkMenu();
std::cin >> selection;
}
return selection;
}
bool isValidMenu(int selection){
if((selection > 0) && (selection < MENU_OPTIONS::SIZE)){
bool isValidBenchmarkMenu(int selection){
if((selection > 0) && (selection < BENCHMARK_OPTIONS::BENCHMARK_SIZE)){
return true;
}
else{
@@ -168,3 +169,5 @@ unsigned int getNumberOfTimesToRun(){
}
return numOfTimesToRun;
}
#endif //BENCHMARK_HPP

View File

@@ -8,9 +8,9 @@
#include <iostream>
#include <vector>
#include "benchmark.hpp"
#include "Algorithms.hpp"
#include "Headers/Problem.hpp"
#include "Problems.hpp"
#include "ProblemSelection.hpp"
@@ -27,7 +27,7 @@ unsigned int getProblemNumber(); //A helper function to error check the problem
void listProblems(); //Lists the problem numbers that you can choose
//Setup the menu options
enum MenuOptions {SOLVE = 1, DESCRIPTION, LIST, EXIT, SIZE};
enum MenuOptions {SOLVE = 1, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE};
int main(){
int selection = 0; //Holds the menu selection of the user
@@ -37,10 +37,11 @@ int main(){
selection = getMenuSelection();
switch(selection){
case SOLVE : solveMenu(); break;
case DESCRIPTION : descriptionMenu(); break;
case LIST : listProblems(); break;
case EXIT : break;
case MenuOptions::SOLVE : solveMenu(); break;
case MenuOptions::DESCRIPTION : descriptionMenu(); break;
case MenuOptions::LIST : listProblems(); break;
case MenuOptions::BENCHMARK : benchmarkMenu(); break;
case MenuOptions::EXIT : break;
}
}while(selection != EXIT);
@@ -51,7 +52,8 @@ void printMenu(){
std::cout << "1. Solve a problem\n"
<< "2. Print a problem description\n"
<< "3. List valid problem numbers\n"
<< "4. Exit" << std::endl;
<< "4. Benchmark\n"
<< "5. Exit" << std::endl;
}
int getMenuSelection(){

View File

@@ -11,12 +11,10 @@ LIBS = $(patsubst %, -lProblem%,$(PROBLEM_NUMBERS))
#Linux makes
all: libsMulti ProjectEuler
libs: directory $(patsubst %, $(LIBDIR)/libProblem%.so,$(PROBLEM_NUMBERS))
benchmark: libsMulti ProjectEulerBenchmark
#Windows makes
windows: allWindows
allWindows: libsWindowsMulti ProjectEuler moveBin
libsWindows: directory $(patsubst %, $(LIBDIR)/libProblem%.a,$(PROBLEM_NUMBERS))
windowsBenchmark: libsWindowsMulti ProjectEulerBenchmark moveBin
#Non-build jobs
directory:
@@ -40,8 +38,6 @@ libsWindowsMulti:
#Building the executable
ProjectEuler: main.cpp
$(CXX) $(EXEFLAGS) -o $@.exe $< -L $(LIBDIR) $(LIBS) $(LINKEDLIBS)
ProjectEulerBenchmark: benchmark.cpp
$(CXX) $(EXEFLAGS) -o ProjectEuler.exe $< -L $(LIBDIR) $(LIBS) $(LINKEDLIBS)
#Clean up/Remove all files and folders created