将矢量<对<双精度,双精度>写入文件的最快方法
Fastest way to write vector <pair <double,double> to file
我正试图在一个文件中写入许多x/y双点。
我提出了以下函数,这是迄今为止最快的解决方案。
有没有其他方法可以加快这个过程?
先写入字符串流,然后打开文件,速度得到了很好的提升。
bool printPoints(const vector <pair <double,double> > &points, const string &file)
{
if(points.empty())
return false;
vector <pair <double,double> > const_iterator i;
if(file != "")
{
stringstream ss;
for(i=points.begin(); i != points.end();++i )
{
ss << i->first << " " << i->second << "n";
}
ofstream out(file.c_str());
if(out.fail())
{
out.close();
return false;
}
out << ss.str();
out.close();
}
return true;
}
我对此进行了测试。给stringstream
写信几乎不会给你带来任何好处。用CCD_ 2代替CCD_。
这是我的测试代码:
#include <vector>
#include <utility>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdio>
using namespace std;
bool printPoints(const vector <pair <double,double> > &points, const string &file)
{
if(points.empty())
return false;
vector <pair <double,double> >::const_iterator i;
if(file != "")
{
stringstream ss;
for(i=points.begin(); i != points.end();++i )
{
ss << i->first << " " << i->second << "n";
}
ofstream out(file.c_str());
if(out.fail())
{
out.close();
return false;
}
out << ss.str();
out.close();
}
return true;
}
bool printPoints2(const vector <pair <double,double> > &points, const string &file)
{
if(points.empty())
return false;
vector <pair <double,double> >:: const_iterator i;
if(file != "")
{
ofstream out(file.c_str());
if(out.fail())
{
out.close();
return false;
}
for(i=points.begin(); i != points.end();++i )
{
out << i->first << " " << i->second << "n";
}
out.close();
}
return true;
}
bool printPoints3(const vector <pair <double,double> > &points, const string &file)
{
if(points.empty())
return false;
vector <pair <double,double> >:: const_iterator i;
if(file != "")
{
FILE *out = fopen(file.c_str(), "w");
if(!out)
{
return false;
}
for(i=points.begin(); i != points.end();++i )
{
fprintf(out, "%f %f", i->first, i->second);
}
fclose(out);
}
return true;
}
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
int main()
{
vector <pair <double,double> > v;
unsigned long long t1, t2;
for(int i = 1; i <= 10000000; i++)
{
v.push_back(make_pair<double, double>((double)i, 1.0/i));
}
t1 = rdtsc();
printPoints(v, "points.txt");
t2 = rdtsc();
cout << "time = " << t2 - t1 << endl;
t1 = rdtsc();
printPoints2(v, "points2.txt");
t2 = rdtsc();
cout << "time = " << t2 - t1 << endl;
t1 = rdtsc();
printPoints3(v, "points3.txt");
t2 = rdtsc();
cout << "time = " << t2 - t1 << endl;
}
Results:
time = 55363637480
time = 54413392112
time = 33069402767
显然,结果可能会因处理器类型、内存类型、硬盘系统(或网络驱动器存储)等而异。但我过去也测试过,发现了类似的结果。
坐标的串行化可以通过将输入划分为多个线程,然后连接它们的返回值来并行化。然后将返回的值写入文件。这样我们可以加快这个过程。
您可以通过以二进制格式一次写入所有coord来提高速度
ofstream out(file.c_str(),std::ios_base::binary);
out.write(reinterpret_cast<const char*>(points.begin()),sizeof(double)*2*points.size());
如果点没有连续存储在pair的内存中(对于向量来说),它可能不起作用,那么您可以将它复制到double向量中的单个double向量(x,y,x,y…),然后写入磁盘。
您会考虑内存映射文件吗?只需将必要的数据(当然是序列化表示)复制到mem文件映射返回的内存区域,然后关闭映射。根据我过去的经验,这是一种将大量数据从STL结构传输到文件的非常快速的方法。
typedef pair<double, double> PDD;
namespace std {
inline
ostream&
operator<<(ostream& os, const PDD& p)
{
return os << p.first << ' ' << p.second;
}
}
bool
PrintPoints(const vector<PDD>& points, const string& file)
{
if (points.empty() || file.empty())
return false;
ofstream fout(file.c_str());
if (!fout)
return false;
copy(points.begin(), points.end(), ostream_iterator<PDD>(fout, "n"));
return true;
}
关闭将由ofstream
dtor调用。
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 在 C++ 中将双精度变量写入二进制文件
- 如何从字符串转换为双精度*
- 为什么我的数组双精度函数不起作用?
- 高精度双精度的 Sprintf 格式化问题
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 将指针数组分配给双精度
- C++如何将字符串逐行转换为双精度
- 长双精度C++是IEEE二进制128的实现吗?