简体   繁体   English

提升asio socket:从硬盘读取文件的最快方法?

[英]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. 我的应用程序可以在Windows,Linux和Mac OS上运行。 That is why I use boost alot. 这就是我使用boost的原因。 I use ab for testing. 我用ab进行测试。 I want to get 2 or at least 1.5 times faster on reading and sending files. 我希望在阅读和发送文件时获得2或至少1.5倍的速度。 May be Boost::Iostream can help me any how? 可能是Boost :: Iostream可以帮我任何方法吗?

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, eg 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 . 在Boost.Interprocess中,您可以在此处阅读更多相关信息。

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. 如果您要优化的是通过套接字从磁盘发送文件,请查看sendfile(2),如果您使用的是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. 如果你想坚持使用更像现在的东西,但要调整它,我会尝试将缓冲区设置为少量兆字节,而不是仅仅8 KB。

boost :: asio已经包装了TransmitFile,参见下面的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM