#include <iostream> #include <fstream> #include <filesystem> #include <chrono> #include <string> namespace fs = std::filesystem; std::string readFile(fs::path path) { // Open the stream to 'lock' the file. std::ifstream f{ path }; // Obtain the size of the file. const auto sz = fs::file_size(path); // Create a buffer. std::string result(sz, ' '); // Read the whole file into the buffer. f.read(result.data(), sz); return result; } int main(int argc, char * argv[]) { auto filename = argv[0]; std::ifstream bigFile(filename); const auto MB = 1024 * 1024; constexpr size_t bufferSize = 100 * MB; std::unique_ptr<char[]> buffer(new char[bufferSize]); fs::path p = "traj.xml"; std::cout << "File size = " << fs::file_size(p)/MB << " MB" << '\n'; auto start = std::chrono::high_resolution_clock::now(); std::string str; while (bigFile) { bigFile.read(buffer.get(), bufferSize); str = buffer.get(); // process data in buffer // std::cout << buffer << "\n---\n" } auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> diff = end-start; std::cout << "Time to read in chunks: " << diff.count() << " s\n"; start = std::chrono::high_resolution_clock::now(); readFile(p); end = std::chrono::high_resolution_clock::now(); auto diff1 = end-start; std::cout << "Time to read in whole: " << diff1.count() << " s\n"; std::cout << "diff1/diff: " << diff1 / diff << "\n"; return 0; }