問題描述
我有許多浮點(diǎn)數(shù)形式的數(shù)據(jù)文本文件.我正在尋找用 C++ 讀取它們的最快方法.如果這是最快的,我可以將文件更改為二進(jìn)制文件.
I have numerous text files of data in the form of float numbers. I'm looking for the fastest way to read them in C++. I can change the file to binary if that's the fastest.
如果您能給我提示或?qū)⑽彝扑]給一個有完整解釋的網(wǎng)站,那就太好了.我不知道是否有任何庫可以快速完成工作.即使有任何開源軟件可以完成這項工作,也會有所幫助.
It would be great if you could give me hint or refer me to a website with complete explanation. I don't know whether there is any library that does the work fast. Even if there is any open source software that does the work, that would be helpful.
推薦答案
擁有二進(jìn)制文件是最快的選擇.您不僅可以在單個操作中使用原始 istream::read
直接在數(shù)組中讀取它(非常快),而且如果您的操作系統(tǒng)支持,您甚至可以將文件映射到內(nèi)存中;您可以在 POSIX 系統(tǒng)上使用 open
/mmap
,CreateFile
/CreateFileMapping
/MapViewOfFile
在 Windows 上,甚至是 Boost 跨平臺解決方案(感謝 @Cory Nelson 指出).
Having a binary file is the fastest option. Not only you can read it directly in an array with a raw istream::read
in a single operation (which is very fast), but you can even map the file in memory if your OS supports it; you can use open
/mmap
on POSIX systems, CreateFile
/CreateFileMapping
/MapViewOfFile
on Windows, or even the Boost cross-platform solution (thanks @Cory Nelson for pointing it out).
快速&臟示例,假設(shè)文件包含一些 float
的原始表示:
Quick & dirty examples, assuming the file contains the raw representation of some float
s:
正常"閱讀:
使用共享內(nèi)存:
這篇關(guān)于讀取輸入文件,最快的方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!