Add time measurement

This commit is contained in:
Robin Dietzel 2023-11-01 10:16:53 +01:00
parent 07bb269175
commit ab129bc6c8

View File

@ -1,6 +1,8 @@
#include <fmt/format.h>
#include <vector>
#include <fstream>
#include <string>
#include <chrono>
#include <mergesort.h>
@ -20,43 +22,45 @@ Does α depend on the size of the input? If it does, how should you modify
your predictions and their graphical illustration?
*/
template<typename T>
auto parse_file(std::ifstream &stream, std::vector<T> &vec) -> void {
std::string buf;
T convbuf;
while (std::getline(stream, buf)) {
convbuf = static_cast<T>(std::stoul(buf));
vec.emplace_back(std::move(convbuf));
}
}
int main(int argc, char *argv[]) {
try {
std::ifstream file("dataset.dat", std::ios_base::in);
if (!file.is_open()) {
fmt::print("Error opening file");
return -1;
}
fmt::print("Opened file {} sucessfully!\n", "dummy");
std::vector<int32_t> dataset;
std::vector<int32_t> dataset{10, 3, 244, 23, 293, 2393, 302};
parse_file(file, dataset);
fmt::print("Read {} values from {}\n", dataset.size(), "dummy");
// std::vector<int32_t> dataset;
// int counter = 0;
//
// while(!file.eof()) {
// int32_t buf;
// file >> buf;
// dataset.emplace_back(buf);
// }
//
// fmt::print("Read {} values from {}\n", dataset.size(), "dummy");
auto t1 = std::chrono::high_resolution_clock::now();
algo::mergesort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) {
return (a>b);
return (a > b);
});
fmt::print("sorted");
auto t2 = std::chrono::high_resolution_clock::now();
// QFile outfile("dataset-sorted.dat");
// if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
// fmt::print("Error! Could not create output file");
// }
//
// for(auto &val : dataset) {
// outfile.write(fmt::format("{}\n", val).c_str());
// }
//
// file.close();
// outfile.flush();
// outfile.close();
auto delay_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fmt::print("Sorted {} entries within {} ms.", dataset.size(), delay_ms.count());
return 0;