benchmarking 1/2
All checks were successful
Linux arm64 / Build (push) Successful in 1m54s

This commit is contained in:
2025-05-26 19:32:31 +02:00
parent 69c07abcec
commit 0bb25d4628
4 changed files with 92 additions and 11 deletions

View File

@@ -25,22 +25,35 @@ bool PiecesFiles::savePieces(int polyominoSize) const {
}
Generator generator;
std::vector<Polyomino> nMinos = generator.generatePolyominoes(polyominoSize);
std::vector<Polyomino> polyominoes = generator.generatePolyominoes(polyominoSize);
return this->savePieces(polyominoSize, polyominoes);
}
bool PiecesFiles::savePieces(int polyominoSize, std::vector<Polyomino>& polyominoes) const {
std::string filePath;
if (!this->getFilePath(polyominoSize, filePath)) {
return false;
}
std::ofstream piecesFile(filePath, std::ios::trunc | std::ios::binary);
if (!piecesFile.good()) {
return false;
}
// sorting the polyominoes is done after setting spawn position to ensure the order is always the same
for (Polyomino& nMino : nMinos) {
for (Polyomino& nMino : polyominoes) {
nMino.goToSpawnPosition();
}
std::sort(nMinos.begin(), nMinos.end());
std::sort(polyominoes.begin(), polyominoes.end());
for (const Polyomino& nMino : nMinos) {
for (const Polyomino& polyomino : polyominoes) {
// write the characteristics of the piece
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + nMino.getLength();
char infoByte = (polyomino.isConvex() << 7) + (polyomino.hasHole() << 6) + polyomino.getLength();
piecesFile.write(&infoByte, 1);
// write the positions of the piece
char positionByte;
for (Position position : nMino.getPositions()) {
for (const Position position : polyomino.getPositions()) {
positionByte = (position.x << 4) + position.y;
piecesFile.write(&positionByte, 1);
}

View File

@@ -22,13 +22,18 @@ class PiecesFiles {
*/
bool savePieces(int polyominoSize) const;
/**
* Generate a file containing all the pieces of the specified size, assuming they have been correctly generated
* @return If the file could be created
*/
bool savePieces(int polyominoSize, std::vector<Polyomino>& polyominoes) const;
/**
* Replace the content of the vectors by the pieces of the specified size, if the file wasn't found the vectors stays untouched
* @return If the file was found
*/
bool loadPieces(int polyominoSize, std::vector<Piece>& pieces, std::vector<int>& convexPieces, std::vector<int>& holelessPieces, std::vector<int>& otherPieces) const;
private:
/**
* Puts the path to the piece file of the specified size in order, if the data folder wasn't found the string stays untouched
* @return If the data folder was found

View File

@@ -3,6 +3,8 @@
#include "TextApp.h"
#include <chrono>
#include <filesystem>
#include <cmath>
void testGeneratorForAllSizes(int amount);
@@ -14,6 +16,8 @@ void generateFilesForOneSize(int size);
void loadFromFilesForOneSize(int size);
void readStatsFromFilesForAllSizes(int amount);
void benchmarking(int max_size);
int main(int argc, char** argv) {
std::srand(std::time(NULL));
@@ -21,8 +25,10 @@ int main(int argc, char** argv) {
// dev: generate files if it hasn't been done before, UI will NOT generate the files
//generateFilesForAllSizes(10);
TextApp UI;
UI.run();
benchmarking(15);
//TextApp UI;
//UI.run();
return 0;
}
@@ -189,3 +195,48 @@ void readStatsFromFilesForAllSizes(int amount) {
std::cout << "Others " << i << "-minos : " << otherPieces.size() << std::endl;
}
}
void benchmarking(int max_size) {
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
std::cout << "| n | Number | Generation | File storing | File retrieving | File size |" << std::endl;
std::cout << "| - | - | - | - | - | - |" << std::endl;
Generator gen;
PiecesFiles pf;
std::vector<Piece> pieces;
std::vector<int> convexPieces;
std::vector<int> holelessPieces;
std::vector<int> otherPieces;
for (int i = 1; i <= max_size; i++) {
std::cout << "| " << i;
auto t1 = high_resolution_clock::now();
auto polyominoes = gen.generatePolyominoes(i);
auto t2 = high_resolution_clock::now();
duration<double, std::milli> ms_double = t2 - t1;
std::cout << " | " << polyominoes.size();
std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms ";
t1 = high_resolution_clock::now();
pf.savePieces(i, polyominoes);
t2 = high_resolution_clock::now();
ms_double = t2 - t1;
std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms ";
t1 = high_resolution_clock::now();
pf.loadPieces(i, pieces, convexPieces, holelessPieces, otherPieces);
t2 = high_resolution_clock::now();
ms_double = t2 - t1;
std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms ";
std::string filePath;
pf.getFilePath(i, filePath);
int fileSize = std::filesystem::file_size(filePath);
std::cout << " | " << fileSize << " bytes |" << std::endl;
}
}