Performance between QFile/fwrite/write in Linux system


Buffer size:

QFile: 16K (Qt 4.8.5, qfile.cpp, QFILE_WRITEBUFFER_SIZE)
fwrite: 8K (http://programmers.stackexchange.com/questions/171315/optimal-buffer-size-for-fread-fwrite)
write: ?

Basically, fwrite is a library which wrapped to call write internally. For writing small chunk of data, fwrite is usually faster than write because it is a buffered scheme. In this link somebody explains the performance difference between fwrite and write for different size of data.

For QFile, we try to track the source by the following code:

QFile aaa("/home/root/test.123");
aaa.open(QIODevice::WriteOnly);
aaa.write("123", 3);
aaa.close();

For QFile.write, it use memcpy to copy data to write buffer. In QFile.close, it flush the data by calling write system call (so we concluded that the write system call doesn't have any buffer?). Of course, implementation of QFile is more complicated than what we mentioned. If the write block is larger than the write buffer size (say 16K in Qt 4.8.5), Qt will use write system call to write data to file storage instead.

It is quite interesting for comparing the performance by not only seeing the execution result, but this tracking doesn't answer to the testing result made by my workmate:

The performance of simple read and write for QFile vs. fread/fwrite
--> read, 6ms vs. 25ms
--> write, 14ms vs. 25ms

The testing data is 256 bytes and the code read/write one byte a time by QFile.read/QFile.write or fread/fwrite. For my limited knowledge, the fread/fwrite should be the fastest way in Linux system.

So, there should be something out of my limited knowledge?

留言

熱門文章