怎样将一百万个浮点型的数据快速的写入到文本文件中?

我做的是一个高速数据采集程序,所以对保存数据的时间有苛刻的要求。
10秒钟就有2000万个浮点型的数据,我的保存时间只允许50秒钟,也就是说我要用小于50秒钟的时间内要往txt文件里写入2000万个浮点型的数据,目前想到的是用循环法把2000万个点连成一个长度为2000万的字符串,然后一次写入长度为2000万的字符串。
遇到的问题是:写入2000万个数据太耗时间了,有没有什么批量写入数据的方法,减少循环次数,或者什么高效的写大量数据的方法?
程序如下:求高手赐教

#include<stdio.h>
#include<afx.h>
#define TOTAL 10

void WriteData(CString,double *);
double data[TOTAL];

void main()
{
long i,a=1;
for(i=1;i<=TOTAL;i++)
{
data[i]=i;
printf("data=%f\n",data[i]);
}
WriteData("D:\\log.txt",data);
}

void WriteData(CString strPath,double * q)//写文件的函数
{
// 1 Open the file and move pointer to the end of the file
CStdioFile pFile;
pFile.Open(strPath,CFile::modeWrite | CFile::typeText | CFile::modeCreate | CFile::modeNoTruncate);
pFile.SeekToEnd();
// 2 Write current time and the name of the current model
CString s;
s = CTime::GetCurrentTime().Format("\n%m-%d-%Y %H:%M:%S\ndata:\n");
CString temp;
// 3 Write the digits, each in a row
for(long i = 0 ; i <TOTAL; i ++)
{
temp.Format("%f\n",q[i]);
s += temp;
}
//write entire string at one time
pFile.WriteString(s);
pFile.Close();
}
数据卡是10秒钟采集了2000万个浮点数据后,自动保存到我自己C语言创建的数组中,我要做的就是用小于50秒的时间把 数组中2000万个浮点数据写到txt文本上。1楼说不要存字符串,直接存数据,是什么意思?是把浮点型的数据以机器码的形式保存在txt文件中吗?

//没啥高科技,编译release,这个2000万也就消耗我9秒不到

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <assert.h>

#include <time.h>

//如果你用的是VC6,这个宏对于习惯标准语法的人很管用

#define for if(false);else for

// lpszFilePath 文件名

// pData 浮点数数组

// iLength 浮点数个数

void writeData(const char * const lpszFilePath, const double * const pData, const int iLength)

{

 int iFileSize=0;

 FILE *pFile=fopen(lpszFilePath,"w");

 assert(pFile!=NULL);

 //申请32MB的缓冲

 char * const pBuffer=(char *)malloc(1024*1024*32);

 assert(pBuffer!=NULL);

 char * const pBufferEnd=pBuffer+1024*1024*32-100;

 char * pCurrent=pBuffer;

 for (int i=0;i<iLength;i++)

 {

  if(pCurrent>=pBufferEnd)

  {

   fwrite(pBuffer,pCurrent-pBuffer,1,pFile);

   pCurrent=pBuffer;

  }

  pCurrent+=sprintf(pCurrent,"%e",pData[i]);

  pCurrent[0]='\n';

  pCurrent++;

 }

 fwrite(pBuffer,pCurrent-pBuffer,1,pFile);

 fclose(pFile);

 free(pBuffer);

}

int main()

{

 clock_t start, finish;

 start=clock();

 double *a=new double[20000000];

 writeData("c:\\1.txt",a,20000000);

 finish=clock();

 printf("消耗时间:%.2f 秒", (double)(finish-start)/CLOCKS_PER_SEC );

 delete[] a;

 return 0;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-10
说下思路:
1.在内存中开辟一个足够大的空间(如20M的内存空间),可以用new的办法来创建此空间.
2.将此空间分成几个部分(如分为5个部分,则每个部分有4M大小).
3.创建一个线程,专门负责将数据写入文件.
4.当数据到达时,先放在第一个内存空间中,当数据填满第一个部分时,将此部分数据一次性地传递给专门写数据的线程,写入文件,写好后将第一个内存部分清空,对在此期间同时到达的数据放到第二个内存空间中,当第二个内存空间填满时,将第二部分数据写入文件,写好后清空第二部分空间,以此类推...,当最后一个内存空间填满后,将最后一个内存空间的数据写到文件,对同时到达的数据保存到第一个内存空间中,如此循环即可....
5.内存空间需要量计算:
一个浮点数4个字节,2000万个浮点数就需要约76.29M空间,也就是说,10秒钟就有约77M的数据,对于4200转/分钟的普通硬盘,一般在15M/s~32M/s,取个平均值20M/S来计算,10秒可以写入200M数据.对于10秒产生77M左右的数据量来说,写入速度是可以满足的.(说明,写磁盘文件时,一次性写入数据量越大,性能越好).
所以,你程序中可以分配100M的内存空间,分为10个部分,10M数据量写入一次磁盘即可.
第2个回答  2010-09-12
srand((unsigned)GetCurrentTime());
vector<double> dvec;
for (size_t i = 0 ; i != 2000 ; ++i)
for(size_t j = 0 ; j != 10000 ; ++j)
{
dvec.push_back(rand());
}
cout<<dvec.size()<<endl;
cout<<"开始写入"<<endl;
DWORD dwTime = GetTickCount();
HANDLE hFile = CreateFile(_T("f:\\1.txt"),
GENERIC_WRITE,
NULL,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
cout<<"错了"<<endl;
}
DWORD dwSize = dvec.size();
WriteFile(hFile,(LPVOID)(&dvec[0]),dwSize,&dwSize,NULL);
if (dwSize != dvec.size())
{
cout<<"error"<<endl;
}
CloseHandle (hFile);
cout<<GetTickCount()-dwTime<<endl;
在我的机子上gettickcount获取的时间只有63。写入20000万个数据应该是很快的。
获取的时候只要声明一个double arr[20000000]数组。将文件读进来就可以了。
记得要有足够的内存,否则程序会挂掉。如果有必要的话,建议检测vector的长度,没500w的时候就写入一次。
第3个回答  2010-09-10
关键是你用什么来进行采集,存储。
用DSP?ARM?存在SD卡上?

如果是微机上的话,这还是很容易的,但注意不要存字符串,直接存数据就可以了。
2E7个双精度浮点数据也就150M。

#include <stdio.h>
#include <stdlib.h>
typedef unsigned __int64 U64;
unsigned __int64 GetCycle ( void )
{
__asm _emit 15
__asm _emit 49
}

double RandDouble ( void )
{
double ret;
typedef union
{
double d;
struct
{
short a,b,c,d;
}i;
}data;
data *p;
p = (data*)&ret;
p->i.a = rand();
p->i.b = rand();
p->i.c = rand();
p->i.d = rand();
return ret;
}

int main ( void )
{
FILE *fp;
U64 t = GetCycle();
double df;
int i;
srand(t);
fp = fopen ( "tmp.txt", "wb" );
for ( i = 2E7; i > 0; i-- )
{
df = RandDouble();
fwrite ( &df, 8, 1, fp );
}
t -= GetCycle();
t = -t;
printf ( "%I64X\n", t );
fclose ( fp );
}

这在我这里跑一边,消耗17643120480个cycle,约为16.43G,我这里3G的CPU,大概5秒跑完,我这里还包括了随机。所以如果是微机的话完全可以
相似回答