From ab129bc6c8b64cb55ddbe3ccbc05736aba579bc5 Mon Sep 17 00:00:00 2001 From: robtor Date: Wed, 1 Nov 2023 10:16:53 +0100 Subject: [PATCH] Add time measurement --- task1/main.cpp | 54 +++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/task1/main.cpp b/task1/main.cpp index 8027720..a720003 100644 --- a/task1/main.cpp +++ b/task1/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include @@ -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 +auto parse_file(std::ifstream &stream, std::vector &vec) -> void { + std::string buf; + T convbuf; + + while (std::getline(stream, buf)) { + + convbuf = static_cast(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 dataset; - std::vector dataset{10, 3, 244, 23, 293, 2393, 302}; + parse_file(file, dataset); + fmt::print("Read {} values from {}\n", dataset.size(), "dummy"); -// std::vector 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(t2 - t1); + fmt::print("Sorted {} entries within {} ms.", dataset.size(), delay_ms.count()); return 0;