diff options
| author | KunoiSayami <[email protected]> | 2023-06-04 19:35:54 +0800 |
|---|---|---|
| committer | KunoiSayami <[email protected]> | 2023-06-04 19:35:54 +0800 |
| commit | 155b3a832c590c55ea3d89dd0783713f1d0f19c1 (patch) | |
| tree | dfeb770865c77f76cf1ab5448f628b7e21bec483 /expt_0604.cpp | |
| parent | 1fbb943005ad78335f0b921be516b3df8140406d (diff) | |
fix: Fix compile error
Signed-off-by: KunoiSayami <[email protected]>
Diffstat (limited to 'expt_0604.cpp')
| -rw-r--r-- | expt_0604.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
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 <cassert> +#include <chrono> +#include <cstdio> +#include <fstream> +#include <iomanip> +#include <iostream> +#include <string> + +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"; +} |
