Skip to content

Instantly share code, notes, and snippets.

@dobrokot
Created March 13, 2013 11:35
Show Gist options
  • Save dobrokot/5151284 to your computer and use it in GitHub Desktop.
Save dobrokot/5151284 to your computer and use it in GitHub Desktop.
Testing speed of ofstream and FILE*
#include <iostream>
#include <fstream>
#include <stdio.h>
const int N = 1000*1000*10;
void f1() {
std::ofstream out("out.txt", std::ios::binary);
for (int i = 0; i < N; ++i) {
out << i << ' ' << i << ' ' << i << '\n';
}
}
void f2() {
FILE *out = fopen("out.txt", "wb");
for (int i = 0; i < N; ++i) {
fprintf(out, "%d %d %d\n", i, i, i);
}
fclose(out);
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "invalid parameters");
return 1;
}
if (argv[1][0] == '1')
f1();
else if (argv[1][0] == '2')
f2();
else {
fprintf(stderr, "invalid parameters");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment