c++ fastest way to write vector <pair <double,double> to file -


i'm trying write lot of x/y double points file.

i came following function fastest solution, yet.

are there other ways speed process?

writing stringstream first , opening file gave nice speed boost.

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(); != 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; } 

i tested this. writing stringstream buys next nothing. using file * instead of fstream give reasonable improvement.

here's test-code:

#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(); != 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(); != 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(); != 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 = 1; <= 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 

obviously, results may vary depending on processor type, memory type, hard disk system (or network drive storage), etc, etc. i've tested in past, , found similar results.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -