最快的方式来写矢量 提交

问题描述 投票:3回答:5

我正在尝试将大量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;
}
c++ file vector std-pair
5个回答
1
投票

我测试了这个。写给stringstream几乎没有任何东西。使用FILE *而不是fstream确实给出了合理的改进。

这是我的测试代码:

#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

显然,结果可能会有所不同,具体取决于处理器类型,内存类型,硬盘系统(或网络驱动器存储)等等。但我过去已对此进行了测试,并发现了类似的结果。


1
投票

通过将输入分成几个线程然后连接它们的返回值,可以并行化坐标的序列化。然后将返回的值写入文件。这样我们就可以加快这个过程。


1
投票

您可以通过以二进制格式一次写入所有coord来获得一些速度提升

ofstream out(file.c_str(),std::ios_base::binary);
out.write(reinterpret_cast<const char*>(points.begin()),sizeof(double)*2*points.size());

如果点不是在存储器中有条件地存储用于对(对于它们是矢量),它可能不起作用,那么你可以将它复制到double的向量中的双倍优先(x,y,x,y ...)的单个向量,接下来写入磁盘。


0
投票

你会考虑内存映射文件吗?只需将必要的数据(当然是序列化表示)复制到内存文件映射返回的内存区域,然后关闭映射。根据我过去的经验,这是将大量数据从STL结构传输到文件的快速方法。


0
投票
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调用。

© www.soinside.com 2019 - 2024. All rights reserved.