summaryrefslogtreecommitdiff
path: root/expt_0604.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'expt_0604.cpp')
-rw-r--r--expt_0604.cpp58
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";
+}