标准c++如何将map里数据高速写入txt文本

knowxy 2008-12-20 07:02:21
		map<string,string>::const_iterator map_it=map_new.begin() ;
while(map_it!=map_new.end())
{
out<<map_it->second<<"\t"<< map_it->first<<endl ;
map_it++;
}

在map里有十万条数据,我想把它写入记事本,按照上面的方法,太慢了!我应该怎么写,才能提高速度?请高手指点
...全文
726 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aaronico 2008-12-22
  • 打赏
  • 举报
回复
先读出来,再写入文件
SearchLife 2008-12-22
  • 打赏
  • 举报
回复
UP
knowxy 2008-12-22
  • 打赏
  • 举报
回复
小弟刚入门;麻烦前辈给段标准代码,学习一下!
realyigo 2008-12-22
  • 打赏
  • 举报
回复 1
给你一段强劲的代码, 它将会使你感觉像在飞.
HANDLE hFile=CreateFileA("out2.txt",GENERIC_WRITE|GENERIC_READ,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
HANDLE hFileMap=CreateFileMappingA(hFile,NULL,PAGE_READWRITE,0,1024*1024*1024,NULL);
PVOID pvFile=MapViewOfFile(hFileMap,FILE_MAP_WRITE,0,0,0);
char * curIndex=(char *)pvFile;
for(map<string,string>::iterator iter=strMap.begin();iter!=strMap.end();iter++)
{
strcpy(curIndex,iter->second.c_str());
curIndex+=iter->second.length();
*curIndex='\t';
curIndex++;
strcpy(curIndex,iter->first.c_str());
curIndex+=iter->first.length();
*curIndex='\r';
curIndex++;
*curIndex='\n';
curIndex++;
}
int fileSize=curIndex-pvFile;
UnmapViewOfFile(pvFile);
CloseHandle(hFileMap);
SetFilePointer(hFile,fileSize,0,FILE_BEGIN);
SetEndOfFile(hFile);
CloseHandle(hFile);
它将strMap输出到out2.txt
需要include windows.h
qqlpp 2008-12-22
  • 打赏
  • 举报
回复
ding
realyigo 2008-12-22
  • 打赏
  • 举报
回复
标准c++只能这样了.
工业级的大文件操作都是直接内存映射.
逸学堂 2008-12-22
  • 打赏
  • 举报
回复
我做了如下实现
[BanUBB][code=C++]
setUp();
map<string,string>::const_iterator map_it=mus.begin() ;
while(map_it != mus.end())
{
//ifs<<map_it->second<<"\t"<< map_it->first<<"\n" ;
ifs<<map_it->second<<"\t"<< map_it->first<<endl ;
map_it++;
}
tearDown();
[/code][/BanUBB]
另外还有
[BanUBB][code=C++]
setUp();
map<string,string>::const_iterator map_it=mus.begin() ;
string str;
str.reserve(1024*1024*10); // 预留10M空间,相对于开辟大内存
while(map_it != mus.end())
{
str+=map_it->second+"\t"+map_it->first+"\n";
map_it++;
}
ifs << str;
tearDown();
[/code][/BanUBB]
他们的性能如下
//ifs<<map_it->second<<"\t"<< map_it->first<<"\n" ;
这种方式性能
[0.385981 s]

ifs<<map_it->second<<"\t"<< map_it->first<<endl ;
这种方式性能
[2.71995 s]
由上可见endl操作非常影响性能,所以尽量避免使用。

第三种,类似开辟大内存方式性能
[1.97383 s]

以上测试时linux是的虚拟机系统
bobye1230 2008-12-22
  • 打赏
  • 举报
回复
顶3楼。
逸学堂 2008-12-22
  • 打赏
  • 举报
回复
使用boost的Serialization测试一下
简单代码
[BanUBB]

#include <fstream>
#include <boost/serialization/string.hpp>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/lexical_cast.hpp>
#include <sys/time.h>

using namespace std;

struct timeval begin;

void setUp()
{
gettimeofday(&begin,0);
}
void tearDown()
{
struct timeval end;
double elapsed;

gettimeofday(&end,0);
elapsed=(end.tv_sec-begin.tv_sec)+(end.tv_usec-begin.tv_usec)/1000000.0;
std::cerr<<" ["<<elapsed<<" s] " << endl;
}

typedef map<string, string> mymap;
// 写入168个‘a’,csdn不让发这么多的a啊
string strTest("aaaaa...");

main ( int argc, char *argv[] )
{
string filepath = "test.ini";

std::ofstream ifs((char *)(filepath.c_str()));
boost::archive::text_oarchive oa(ifs);

mymap mus;
// map中写入20w数据,每个value为168个字节。
for(int i = 0; i < 200000; ++i)
{
mus.insert(make_pair(boost::lexical_cast<string>(i),strTest));
}

setUp(); // 开始计时
oa & mus; // 写入数据
tearDown(); // 显示计时
return 0
}
[/BanUBB]
测试三次的性能如下
[0.47578 s]
[0.472901 s]
[0.473222 s]

不过这种方法,需要你自己编译boost::serialization库,比较适合读取写入文件数据。
不知道性能能否满足你的需求,如果使用binary模式存储可能速度会更快一些。
以上数据是我在虚拟机下的测试结果。
Gary@Tokyo 2008-12-22
  • 打赏
  • 举报
回复
mark
knowxy 2008-12-22
  • 打赏
  • 举报
回复
      map<string,string>::const_iterator map_it=map_new.begin() ;
string str="";
while(map_it!=map_new.end())
{
str+=map_it->second+"\t"+ map_it->first;
map_it++;
}
out.write(str.c_str(),str.length());

我这样,速度快了点。

请高人帮我实现 3楼的方法!
haosuai 2008-12-22
  • 打赏
  • 举报
回复
关于这个速度的问题,我也做过一些。
现在一般都是用空间来换时间的做法,针对这个问题可以创建一块共享内存,把MAP中的数据按照输出格式分为多个流,放到这个共享内存中,然后可以开多个进程分别对共享内存的不同的流进行读取,分别将这些流写进文件。
不过这样做代码量很大,而且多个进程的通信也不是很好控制。
Kratos 2008-12-22
  • 打赏
  • 举报
回复
3L的方法不错。
csgdseed 2008-12-22
  • 打赏
  • 举报
回复
顶3楼的方法
taifeng123 2008-12-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tangshuiling 的回复:]
开辟一个大的内存缓冲区,将读入的map数据暂存在内存缓冲区,再一次性回写,按照这样的方式应该比楼主的逐条写入快几个数量级
[/Quote]
就这个方法
xuruichen 2008-12-22
  • 打赏
  • 举报
回复
感觉代码很正确,
写到文件里,需要很长时间吗?
没有尝试过。
星羽 2008-12-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tangshuiling 的回复:]
开辟一个大的内存缓冲区,将读入的map数据暂存在内存缓冲区,再一次性回写,按照这样的方式应该比楼主的逐条写入快几个数量级
[/Quote]

try
tangshuiling 2008-12-20
  • 打赏
  • 举报
回复
开辟一个大的内存缓冲区,将读入的map数据暂存在内存缓冲区,再一次性回写,按照这样的方式应该比楼主的逐条写入快几个数量级
lzr4304061988012 2008-12-20
  • 打赏
  • 举报
回复
没发现还有什么办法,关注。
nullah 2008-12-20
  • 打赏
  • 举报
回复
MARK
顶~~~~

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧