Created
August 24, 2017 08:20
-
-
Save odeblic/69ebec2a03801630b09395ae50736f95 to your computer and use it in GitHub Desktop.
Cost of flushing files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
#include <cstdio> | |
#include <iostream> | |
void write_file(std::FILE* fd) | |
{ | |
for (int i=0; i<1000000; i++) | |
{ | |
std::fprintf(fd, "iteration no %d (last bit=%d)\n", i, i % 2); | |
} | |
} | |
void write_file_and_flush(std::FILE* fd) | |
{ | |
for (int i=0; i<1000000; i++) | |
{ | |
std::fprintf(fd, "iteration no %d (last bit=%d)\n", i, i % 2); | |
std::fflush(fd); | |
} | |
} | |
int main() | |
{ | |
auto f1 = std::fopen("test_1.txt", "a"); | |
auto f2 = std::fopen("test_2.txt", "a"); | |
auto start1 = std::chrono::system_clock::now(); | |
write_file(f1); | |
auto end1 = std::chrono::system_clock::now(); | |
auto start2 = std::chrono::system_clock::now(); | |
write_file_and_flush(f2); | |
auto end2 = std::chrono::system_clock::now(); | |
std::fclose(f1); | |
std::fclose(f2); | |
std::cout << "without flushing : " << (end1 - start1).count() << " ns\n"; | |
std::cout << "with flushing : " << (end2 - start2).count() << " ns\n"; | |
} | |
/* | |
results: | |
without flushing : 297908807 ns | |
with flushing : 945402154 ns | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment