mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
Refactored to make benchmark part of the main program
This commit is contained in:
@@ -5,9 +5,43 @@
|
|||||||
//This is a header file with a few functions to help select and run problems
|
//This is a header file with a few functions to help select and run problems
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef PROBLEMSELECTION_HPP
|
||||||
|
#define PROBLEMSELECTION_HPP
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Problems.hpp"
|
|
||||||
#include "Algorithms.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
|
//Setup the problem numbers
|
||||||
@@ -92,4 +126,6 @@ void listProblems(){
|
|||||||
std::cout << ", " << PROBLEM_NUMBERS[problemNumber];
|
std::cout << ", " << PROBLEM_NUMBERS[problemNumber];
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif //PROBLEMSELECTION_HPP
|
||||||
|
|||||||
32
Problems.hpp
32
Problems.hpp
@@ -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"
|
|
||||||
@@ -1,65 +1,66 @@
|
|||||||
//ProjectEulerCPP/benchmark.cpp
|
//ProjectEulerCPP/benchmark.hpp
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 07-08-20
|
// Created: 07-08-20
|
||||||
//Modified: 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
|
//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 <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "ProblemSelection.hpp"
|
#include "ProblemSelection.hpp"
|
||||||
|
|
||||||
|
|
||||||
void printMenu(); //Prints the menu to the screen
|
void printBenchmarkMenu(); //Prints the menu to the screen
|
||||||
int getMenuSelection(); //Returns a valid menu option
|
int getBenchmarkMenuSelection(); //Returns a valid menu option
|
||||||
bool isValidMenu(int selection); //Determines if a value is a valid menu option. Helper for getMenuSelection
|
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 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 runAllShort(); //Runs all problems except a few that are specified because of run length
|
||||||
void runAll(); //Runs all problems
|
void runAll(); //Runs all problems
|
||||||
unsigned int getNumberOfTimesToRun(); //Asks how many times a problem is supposed to run and returns the value
|
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};
|
std::vector<unsigned int> tooLong = {15};
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
void benchmarkMenu(){
|
||||||
int selection = 0;
|
int selection = 0;
|
||||||
do{
|
|
||||||
printMenu();
|
|
||||||
selection = getMenuSelection();
|
|
||||||
|
|
||||||
switch(selection){
|
printBenchmarkMenu();
|
||||||
case RUN_SPECIFIC: runSpecific(); break;
|
selection = getBenchmarkMenuSelection();
|
||||||
case RUN_ALL_SHORT: runAllShort(); break;
|
|
||||||
case RUN_ALL: runAll(); break;
|
switch(selection){
|
||||||
case EXIT: break;
|
case BENCHMARK_OPTIONS::RUN_SPECIFIC: runSpecific(); break;
|
||||||
}
|
case BENCHMARK_OPTIONS::RUN_ALL_SHORT: runAllShort(); break;
|
||||||
}while(selection != EXIT);
|
case BENCHMARK_OPTIONS::RUN_ALL: runAll(); break;
|
||||||
return 0;
|
case BENCHMARK_OPTIONS::BENCHMARK_EXIT: break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void printMenu(){
|
void printBenchmarkMenu(){
|
||||||
std::cout << "1. Run a specific problem\n"
|
std::cout << "1. Run a specific problem\n"
|
||||||
<< "2. Run all problems that have a reasonably short run time\n"
|
<< "2. Run all problems that have a reasonably short run time\n"
|
||||||
<< "3. Run all problems\n"
|
<< "3. Run all problems\n"
|
||||||
<< "4. Exit the program" << std::endl;
|
<< "4. Exit the program" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getMenuSelection(){
|
int getBenchmarkMenuSelection(){
|
||||||
int selection = 0;
|
int selection = 0;
|
||||||
std::cin >> selection;
|
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::cout << "That is an invalid option!\nPress Enter to continue" << std::endl;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
std::cin.get();
|
std::cin.get();
|
||||||
printMenu();
|
printBenchmarkMenu();
|
||||||
std::cin >> selection;
|
std::cin >> selection;
|
||||||
}
|
}
|
||||||
return selection;
|
return selection;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValidMenu(int selection){
|
bool isValidBenchmarkMenu(int selection){
|
||||||
if((selection > 0) && (selection < MENU_OPTIONS::SIZE)){
|
if((selection > 0) && (selection < BENCHMARK_OPTIONS::BENCHMARK_SIZE)){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@@ -168,3 +169,5 @@ unsigned int getNumberOfTimesToRun(){
|
|||||||
}
|
}
|
||||||
return numOfTimesToRun;
|
return numOfTimesToRun;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif //BENCHMARK_HPP
|
||||||
16
main.cpp
16
main.cpp
@@ -8,9 +8,9 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "benchmark.hpp"
|
||||||
#include "Algorithms.hpp"
|
#include "Algorithms.hpp"
|
||||||
#include "Headers/Problem.hpp"
|
#include "Headers/Problem.hpp"
|
||||||
#include "Problems.hpp"
|
|
||||||
#include "ProblemSelection.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
|
void listProblems(); //Lists the problem numbers that you can choose
|
||||||
|
|
||||||
//Setup the menu options
|
//Setup the menu options
|
||||||
enum MenuOptions {SOLVE = 1, DESCRIPTION, LIST, EXIT, SIZE};
|
enum MenuOptions {SOLVE = 1, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE};
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
int selection = 0; //Holds the menu selection of the user
|
int selection = 0; //Holds the menu selection of the user
|
||||||
@@ -37,10 +37,11 @@ int main(){
|
|||||||
selection = getMenuSelection();
|
selection = getMenuSelection();
|
||||||
|
|
||||||
switch(selection){
|
switch(selection){
|
||||||
case SOLVE : solveMenu(); break;
|
case MenuOptions::SOLVE : solveMenu(); break;
|
||||||
case DESCRIPTION : descriptionMenu(); break;
|
case MenuOptions::DESCRIPTION : descriptionMenu(); break;
|
||||||
case LIST : listProblems(); break;
|
case MenuOptions::LIST : listProblems(); break;
|
||||||
case EXIT : break;
|
case MenuOptions::BENCHMARK : benchmarkMenu(); break;
|
||||||
|
case MenuOptions::EXIT : break;
|
||||||
}
|
}
|
||||||
}while(selection != EXIT);
|
}while(selection != EXIT);
|
||||||
|
|
||||||
@@ -51,7 +52,8 @@ void printMenu(){
|
|||||||
std::cout << "1. Solve a problem\n"
|
std::cout << "1. Solve a problem\n"
|
||||||
<< "2. Print a problem description\n"
|
<< "2. Print a problem description\n"
|
||||||
<< "3. List valid problem numbers\n"
|
<< "3. List valid problem numbers\n"
|
||||||
<< "4. Exit" << std::endl;
|
<< "4. Benchmark\n"
|
||||||
|
<< "5. Exit" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getMenuSelection(){
|
int getMenuSelection(){
|
||||||
|
|||||||
4
makefile
4
makefile
@@ -11,12 +11,10 @@ LIBS = $(patsubst %, -lProblem%,$(PROBLEM_NUMBERS))
|
|||||||
#Linux makes
|
#Linux makes
|
||||||
all: libsMulti ProjectEuler
|
all: libsMulti ProjectEuler
|
||||||
libs: directory $(patsubst %, $(LIBDIR)/libProblem%.so,$(PROBLEM_NUMBERS))
|
libs: directory $(patsubst %, $(LIBDIR)/libProblem%.so,$(PROBLEM_NUMBERS))
|
||||||
benchmark: libsMulti ProjectEulerBenchmark
|
|
||||||
#Windows makes
|
#Windows makes
|
||||||
windows: allWindows
|
windows: allWindows
|
||||||
allWindows: libsWindowsMulti ProjectEuler moveBin
|
allWindows: libsWindowsMulti ProjectEuler moveBin
|
||||||
libsWindows: directory $(patsubst %, $(LIBDIR)/libProblem%.a,$(PROBLEM_NUMBERS))
|
libsWindows: directory $(patsubst %, $(LIBDIR)/libProblem%.a,$(PROBLEM_NUMBERS))
|
||||||
windowsBenchmark: libsWindowsMulti ProjectEulerBenchmark moveBin
|
|
||||||
|
|
||||||
#Non-build jobs
|
#Non-build jobs
|
||||||
directory:
|
directory:
|
||||||
@@ -40,8 +38,6 @@ libsWindowsMulti:
|
|||||||
#Building the executable
|
#Building the executable
|
||||||
ProjectEuler: main.cpp
|
ProjectEuler: main.cpp
|
||||||
$(CXX) $(EXEFLAGS) -o $@.exe $< -L $(LIBDIR) $(LIBS) $(LINKEDLIBS)
|
$(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
|
#Clean up/Remove all files and folders created
|
||||||
|
|||||||
Reference in New Issue
Block a user