// 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"; }