读取输入文件,最快的方式可能吗? [英] read input files, fastest way possible?

查看:149
本文介绍了读取输入文件,最快的方式可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浮点数形式的数据的众多的文本文件。我正在寻找在C ++中读取它们的最快方式。我可以更改该文件为二进制如果这是最快的。

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.

这将是巨大的,如果你可以给我暗示或参考我完全解释的网站。我不知道是否存在做的工作快任何库。即使有,做工作的任何开源软件,这将是有益的。

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.

推荐答案

有一个二进制文件是最快的选项。您不仅可以在一个阵列直接读取原始的的IStream ::在一次操作中读(这是非常快的),但你甚至可以映射内存中的文件如果你的操作系统支持它;您可以使用打开 / MMAP 在POSIX系统,的CreateFile / 的CreateFileMapping / MapViewOfFile 在Windows上,甚至加速跨平台解决方案(感谢@Cory纳尔逊指出来)。

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).

快速和放大器;肮脏的例子,假设该文件包含一些浮动的原料再presentation 取值:

Quick & dirty examples, assuming the file contains the raw representation of some floats:

正常读

#include <fstream>
#include <vector>

// ...

// Open the stream
std::ifstream is("input.dat");
// Determine the file length
is.seekg(0, std:ios_base::end);
std::size_t size=is.tellg();
is.seekg(0, std::ios_base::begin);
// Create a vector to store the data
std::vector<float> v(size/sizeof(float));
// Load the data
is.read((char*) &v[0], size);
// Close the file
is.close();

使用共享内存:

#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>

using boost::interprocess;

// ....

// Create the file mapping
file_mapping fm("input.dat", read_only);
// Map the file in memory
mapped_region region(fm, read_only);
// Get the address where the file has been mapped
float * addr = (float *)region.get_address();
std::size_t elements  = region.get_size()/sizeof(float);

这篇关于读取输入文件,最快的方式可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆