From 155b3a832c590c55ea3d89dd0783713f1d0f19c1 Mon Sep 17 00:00:00 2001 From: KunoiSayami Date: Sun, 4 Jun 2023 19:35:54 +0800 Subject: fix: Fix compile error Signed-off-by: KunoiSayami --- expt_0604.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 expt_0604.cpp (limited to 'expt_0604.cpp') diff --git a/expt_0604.cpp b/expt_0604.cpp new file mode 100644 index 0000000..9217f9e --- /dev/null +++ b/expt_0604.cpp @@ -0,0 +1,58 @@ +// Experimental content: Test fstream vs stdio + +#include +#include +#include +#include +#include +#include +#include + +constexpr auto max_limit = 0x7fffffff; +auto limit = 0L; +auto filename = "normal_distribution.txt"; + +void stdio() { + auto file = std::fopen(filename, "r"); + assert(file); + auto read_count = 0L; + for (unsigned long long l; + read_count < limit && fscanf(file, "%lld ", &l) != EOF;) + read_count++; + fclose(file); +} + +void fstream() { + std::ifstream fin(filename); + auto read_count = 0L; + for (unsigned long long l; read_count < limit && !fin.eof(); fin >> l) + read_count++; + fin.close(); +} + +auto measure(const char *function_name, void (*test_function)()) { + auto start = std::chrono::high_resolution_clock::now(); + test_function(); + auto end = std::chrono::high_resolution_clock ::now(); + auto duration = end - start; + std::cout << "Function: " << std::fixed << std::setprecision(3) + << function_name << " " << (double)duration.count() / 1000.0 << "ms" + << std::endl; + return duration.count(); +} + +int main(int argc, char const *argv[]) { + + if (argc >= 2) { + filename = argv[1]; + } + if (argc >= 3) { + limit = strtol(argv[2], nullptr, 10); + } + if (limit <= 0) { + limit = max_limit; + } + auto s = measure("stdio", stdio); + auto f = measure("fstream", fstream); + std::cout << (s > f ? "stdio" : "fstream") << "win"; +} -- cgit v1.3.1