Boost asio socket: fastest way to read file from hard drive?
So I have tried:
int buff_length = 8192;
ifstream stream;
char* buffer = new char[buff_length];
stream.open( path.string().c_str(), ios::binary );
boost::system::error_code ignored_error;
while (stream)
{
stream.read(buffer, buff_length);
boost::asio::write(*socket, boost::asio::buffer(buffer, stream.gcount()),
boost::asio::transfer_all(), ignored_error);
}
I wonder how you do it - how to do it faster?
My app shall work across Windows, linux and mac os. That is why I use boost alot. I use ab开发者_JS百科
for testing. I want to get 2 or at least 1.5 times faster on reading and sending files. May be Boost::Iostream can help me any how?
Not entirely sure what you're after, but to answer your question about fast reading of files you can map the file into memory.
This way you read from the memory instead of from disk. Depending on the file size and such there are different approaches that might be interesting, e.g. map whole file if small or map regions of file throughout the file as you process it if it's a large file.
In Boost.Interprocess you can read more about this here.
If what you're trying to optimize is sending a file from disk across a socket, check out sendfile(2) if you're on Linux. It is specifically designed for this purpose.
If you want to stick with something more like what you have now, but tune it, I'd try making the buffer be a small number of megabytes rather than just 8 KB.
boost::asio has already wrapped TransmitFile, see the following example
精彩评论