File writing performance comparing
-
Tested by the sample code below, the result: 1.
write
is much slower than the other two 2.fwrite
is twice as fast asofstream.write
I knowwrite
is not buffered, so it is much slower, I think the performance of the other two should be close, but why not?#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <tchar.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
#include <fstream>
#include <windows.h>#pragma warning(disable : 4996)
int _tmain(int argc, _TCHAR* argv[])
{
const int buf_size = 1<<20;
char * buf = new char[buf_size];
FILE * f1 = fopen("d:/test1", "w");
assert(f1);
int f2 = open("d:/test2", _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IWRITE);
assert(f2!=-1);DWORD t; t = GetTickCount(); for (size\_t i=0; i<buf\_size; ++i) fwrite(buf+i, 1, 1, f1); fclose(f1); printf("count of ticks used: %d bytes\\n", GetTickCount()-t); t = GetTickCount(); for (int i=0; i<buf\_size; ++i) write(f2, buf+i, 1); close(f2); printf("count of ticks used: %d\\n", GetTickCount()-t); t = GetTickCount(); std::ofstream ofs("d:/test3", std::ios\_base::out | std::ios\_base::binary | std::ios\_base::trunc); for (int i=0; i<buf\_size; ++i) ofs.write(buf+i, 1); ofs.close(); printf("count of ticks used: %d", GetTickCount()-t); delete buf; getchar(); return 0;
}
-
Tested by the sample code below, the result: 1.
write
is much slower than the other two 2.fwrite
is twice as fast asofstream.write
I knowwrite
is not buffered, so it is much slower, I think the performance of the other two should be close, but why not?#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <tchar.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
#include <fstream>
#include <windows.h>#pragma warning(disable : 4996)
int _tmain(int argc, _TCHAR* argv[])
{
const int buf_size = 1<<20;
char * buf = new char[buf_size];
FILE * f1 = fopen("d:/test1", "w");
assert(f1);
int f2 = open("d:/test2", _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IWRITE);
assert(f2!=-1);DWORD t; t = GetTickCount(); for (size\_t i=0; i<buf\_size; ++i) fwrite(buf+i, 1, 1, f1); fclose(f1); printf("count of ticks used: %d bytes\\n", GetTickCount()-t); t = GetTickCount(); for (int i=0; i<buf\_size; ++i) write(f2, buf+i, 1); close(f2); printf("count of ticks used: %d\\n", GetTickCount()-t); t = GetTickCount(); std::ofstream ofs("d:/test3", std::ios\_base::out | std::ios\_base::binary | std::ios\_base::trunc); for (int i=0; i<buf\_size; ++i) ofs.write(buf+i, 1); ofs.close(); printf("count of ticks used: %d", GetTickCount()-t); delete buf; getchar(); return 0;
}
I don't know enough about fstream - though it looks as though it's performance may vary between implementations. Just ran your code through MinGW 3.4.5, and I see a very different picture to the one you describe. Over here, your code timings are: fstream: 78 fwrite: 109 write: 3391 I know iostream & fstream increase program size considerably, (read several 100k) and would have expected fstream to be second, in terms of performance, though this clearly appears not to be the case. :confused: