将向量<pair <double,double>写入文件的最快方法

用户名

我正在尝试向文件中写入很多x / y双点。

我提出以下功能是最快的解决方案。

还有其他方法可以加快这一过程吗?

首先写入stringstream,然后打开文件,可以大大提高速度。

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;
}
马特·彼得森(Mats Petersson)

我测试了这个。写信给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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

推回静态向量<pair <string,double [9]>> myVector;

来自分类Dev

将 Double 的 SparseArray 写入 Parcel

来自分类Dev

如何将Double与向量相乘?

来自分类Dev

如何将向量乘以Double?

来自分类Dev

用C将整数写入文件的最快方法

来自分类Dev

将Olevariant 2D Double数组转换为Dynamic 2D Double Array的最快方法

来自分类Dev

将double []包装为Double []

来自分类Dev

将 [[[Double]]] 投射回 [Double]

来自分类Dev

将double传递给Graphics fillRect方法

来自分类Dev

如何将double插入main方法?

来自分类Dev

向量化double for循环

来自分类Dev

对pair和double的映射进行排序

来自分类Dev

C ++ 2D向量-将int转换为double

来自分类Dev

C ++:将字符串转换为向量<double>

来自分类Dev

使用点分隔符将double写入文本文件

来自分类Dev

寻找将double-> double C函数包装到Python的最简单方法

来自分类Dev

如何将包含double的std :: string转换为double的向量?

来自分类Dev

错误:无法将“ <lambda(double)>”转换为“ double(*)(double)”

来自分类Dev

在java中将float除以double与将double除以double

来自分类Dev

为double的向量创建排名

来自分类Dev

无法将double [] []转换为double **

来自分类Dev

将double数组绑定为double *&

来自分类Dev

将矩阵乘以向量的最快方法

来自分类Dev

将文件读取到Map <Integer,ArrayList <Double >>

来自分类Dev

用Java将很小的字符串写入文件的最快方法是什么?

来自分类Dev

用Java将很小的字符串写入文件的最快方法是什么?

来自分类Dev

通过错误检查将wstring转换为double的方法

来自分类Dev

将const char *转换为vector <double>更好的方法

来自分类Dev

C:将double舍入为int的更好方法

Related 相关文章

热门标签

归档