Автор: Алекс Кэш (25.11.2011 в 19:39)
RE:можно усокрится, если переопределить....
я так и делаю (где ////!!!!! в коде стоит), но ускорения все равно нет.
В общем старая добрая fprintf и нет мучений ))
мой тестовый вариант:
здесь выводится Imax чисел типа double (=1000000) , j-тое количество раз (=20) для усреднения.
// test viuvod.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#include <cstdlib>
#include <iostream>
#include <time.h>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string.h>
using namespace std;
std::string inline DToStr(double one)
{
std::stringstream ss;
ss<<one;
return ss.str();
}
int main(int argc, char *argv[])
{
const int Jmax=20;const double Imax=1000000;
clock_t t0, t1, t2, t3;
double SredneeTime=0;
int j=0;
string StrTemp;
char szbuff[255];
std::ostringstream ost;
ofstream fout;
cout << "_________________________________________________\n";
cout << "Programm \"Test\"\n";
cout << "_________________________________________________\n\n";
//-----------------------------------------------------------------------------------
//Potokom
/*
t2 = clock();
for (j=0;j<Jmax;j++)
{
fout.open("TestVivod.fout");
if (fout.fail())
{
cout << "Can not open (create) Out file: " << "TestVivod.fout" << endl;
std::cout << "_________________________________________________\n";
system("PAUSE");
return 1;
};
fout << std::setprecision(8) << std::setw(8);
t0 = clock();
//cout << j << " ";
for (double i=0;i<Imax;i++)
{
fout << std::setprecision(8) << std::setw(8);
fout << i << "$";
};
fout << endl ;
t1 = clock();
SredneeTime+=(t1-t0);
fout.close();
};
t3 = clock();
cout << "Srednee Time work function fout = " << (SredneeTime/Jmax) << " In sek=" << ((SredneeTime/Jmax)/CLOCKS_PER_SEC) << endl
<< "Obwee Time Vivoda Potokom = " << (t3-t2) << " In sek=" << ((double)(t3-t2)/CLOCKS_PER_SEC) << endl << endl ;
*/
//-----------------------------------------------------------------------------------
//Potokom s perevodom v string
t2 = clock();
for (j=0;j<Jmax;j++)
{
fout.open("TestVivod.foutstring");
if (fout.fail())
{
cout << "Can not open (create) Out file: " << "TestVivod.fout" << endl;
std::cout << "_________________________________________________\n";
system("PAUSE");
return 1;
};
t0 = clock();
//cout << j << " ";
for (double i=0;i<Imax;i++)
{
sprintf(szbuff, "%.4g", i);
StrTemp += szbuff;
//StrTemp=DToStr(i);
//boost::lexical_cast<std::string>( i )
StrTemp += "$\n";
};
StrTemp += "\n";
fout << StrTemp ; ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!
t1 = clock();
SredneeTime+=(t1-t0);
fout.close();
};
t3 = clock();
cout << "Srednee Time work function fout s preobrazovaniem = " << (SredneeTime/Jmax) << " In sek=" << ((SredneeTime/Jmax)/CLOCKS_PER_SEC) << endl
<< "Obwee Time Vivoda fout s preobrazovaniem = " << (t3-t2) << " In sek=" << ((double)(t3-t2)/CLOCKS_PER_SEC) << endl <<endl;
//------------------------------------------------------------------------------------
//fprintfom
t2 = clock();
SredneeTime=0;
FILE *infile;
for (j=0;j<Jmax;j++)
{
infile = fopen("TestVivod.printf", "w");
if (infile == NULL)
{
std::cout << "Failed to open file: " << "TestVivod.printf" << "\n";
return 2;
};
t0 = clock();
//printf("j=%i ",j);
for (double i=0;i<Imax;i++)
{
fprintf(infile,"%.4g $\n", i);
};
fprintf(infile,"\n");
t1 = clock();
SredneeTime+=(t1-t0);
fclose (infile);
};
t3 = clock();
cout << "Srednee Time work function fprintf = " << (SredneeTime/Jmax) << " In sek=" << ((SredneeTime/Jmax)/CLOCKS_PER_SEC) << endl
<< "Obwee Time Vivoda fprintf = " << (t3-t2) << " In sek=" << ((double)(t3-t2)/CLOCKS_PER_SEC) << endl <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
[code]
|
а насчет жуткого текстового файла в 200 мб - согласен на 100%.
просто это крайний вариант, такой большой объем данных будет выводиться только в некоторых случаях (но их тоже надо учесть). А смысл в том, что дальше этот файл подхватывает другая прога, и она на Perle, так что бинарный файл она не прочитает.
Или может есть еще какие- то способы передачи данных между прогами на Perle и c++ ?