Skip to content

Instantly share code, notes, and snippets.

@dagon666
Created December 4, 2014 21:36
Show Gist options
  • Save dagon666/31d35b3aad25940ab13f to your computer and use it in GitHub Desktop.
Save dagon666/31d35b3aad25940ab13f to your computer and use it in GitHub Desktop.
io_measure
#include <string>
#include <iostream>
#include <chrono>
void synced(const std::string& s, unsigned int n = 0) {
std::cout.sync_with_stdio(true);
while (n--) std::cout << s << std::endl;
}
void unsynced(const std::string& s, unsigned int n = 0) {
std::string tmp = s + "\n";
std::cout.sync_with_stdio(false);
while (n--) std::cout << tmp;
}
int main(int argc, char *argv[]) {
std::string s = "Quite long test string to test the difference in synced and unsynced output stream performance";
unsigned int n = 1000000;
if (argc>=2) {
n = std::strtoul(argv[1], NULL, 10);
}
auto s1 = std::chrono::high_resolution_clock::now();
synced(s, n);
auto e1 = std::chrono::high_resolution_clock::now();
auto s2 = std::chrono::high_resolution_clock::now();
unsynced(s, n);
auto e2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> el1 = e1 - s1;
std::chrono::duration<double> el2 = e2 - s2;
std::cout << "synced: " <<
std::chrono::duration_cast<std::chrono::milliseconds>(el1).count()
<< std::endl;
std::cout << "unsynced: " <<
std::chrono::duration_cast<std::chrono::milliseconds>(el2).count()
<< std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment