`
后生姓王
  • 浏览: 26466 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Windows编程 CString/std::string 比较

    博客分类:
  • C++
阅读更多

前些天看到有评论说标准库的string占内存大,效率低,而且函数不实用。而我正在写一个大量处理字符串的程序,所以不得不自己测试一下了。看看是继续用STL库还是回头再用MFC或者干脆不用库函数了。

 

环境:XP Professional 2002 SP3

Microsoft Visual C++ 6.0

 

Project Settings:首先都采用Win32ConsoleApplication empty project的初始选项

作如下修改,

link:删除/subsystem 开关,其余不变。

C/C++(Code Generation):Use Runtime Library:Debug MultiThread

General:Not Using MFC(虽然我实际上用了,这里不选)

Language:英语(美国)

当然,免费的VC++6.0,不能采用Release编译连接,只好用Debug了。

 

一。文件大小

CString:

 

/*************************
test.h
**************************/
#include <afx.h>//afx.h包含windows.h,于是,就少了#include <windows.h>

/*************************
test.cpp
**************************/
#include "test.h"

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd)
{
	CString s="Hello World!";
	return 0;
}

 

Compile:

 test.obj 6.50KB

 CString.pdb 2249KB

 CString.ilk 1654KB

link:

 CString.exe 1.25MB

 

STLstring:

 

/**********************
test.h
***********************/
#include <windows.h>
#include <string>

/**********************
test.cpp
***********************/
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd)
{
	string s;
	s.assign("Hello World!");
	return 0;
}

 

Compile:

 test.obj 31KB

 STLstring.pdb 577KB

 STLstring.ilk 304KB

link:

 STLstring.exe 236KB

这么看来,就生成的可执行文件大小来说,用STL时小很多。

其实从pdb文件大小就看出来了:CString 所要求的动态链接库比STL string大太多了。

 

二。执行效率

CString:

 

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd)
{
	char* buf;
	buf=new char[128];
	CString s;
	DWORD start,end;
	start=::timeGetTime();
	for(DWORD i=0;i<10000000;i++)
	{
		s=";riusvnaervCs;eriv";
		s.Find("C",0);
	}
	end=::timeGetTime();
	::itoa(end-start,buf,10);
	MessageBox(::GetActiveWindow(),buf,"CString",MB_OK);
	delete []buf;
	return 0;
}

 STLstring:

 

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd)
{
	char* buf;
	buf=new char[128];
	string s;
	DWORD start,end;
	start=::timeGetTime();
	for(DWORD i=0;i<10000000;i++)
	{
		s=";riusvnaervCs;eriv";
		s.find("C",0);
	}
	end=::timeGetTime();
	::itoa(end-start,buf,10);
	MessageBox(::GetActiveWindow(),buf,"STLstring",MB_OK);
	delete []buf;
	return 0;
}

 执行结果:

多次执行,记录如下:

CString   :5258 5117 5195 5204 5184

STLstring:4833 4745 4758 4746 4749

 

显示的数字是循环开始前到循环结束经过的时间(毫秒)。

 

我们再来看另一个实验:

CString:

 

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd)
{
	char* buf;
	buf=new char[128];
	CString s="CCCCCCCCCC";
	DWORD start,end;
	start=::timeGetTime();
	for(DWORD i=0;i<10000000;i++)
	{
		s=s.Mid(1);
		s+='C';
	}
	end=::timeGetTime();
	::itoa(end-start,buf,10);
	MessageBox(::GetActiveWindow(),buf,"CString",MB_OK);
	delete []buf;
	return 0;
}

 STLstring:

 

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd)
{
	char* buf;
	buf=new char[128];
	string s="CCCCCCCCCC";
	DWORD start,end;
	start=::timeGetTime();
	for(DWORD i=0;i<10000000;i++)
	{
		s=s.substr(1);
		s+='C';
	}
	end=::timeGetTime();
	::itoa(end-start,buf,10);
	MessageBox(::GetActiveWindow(),buf,"STLstring",MB_OK);
	delete []buf;
	return 0;
}

 运行结果:

 

STL比MFC的字符串对象稍稍快那么一点。

 

内存上呢?

 

就用上面的实验,循环中:

 

 

 

循环结束至出现提示框:

 

 

 

OK,测试内存占用,我没什么招数,只好用任务管理器了。但是,CString和STL的string的差异也被我发现了。

 

没什么好说的了。

 

CString 除了用起来比较方便,(我最喜欢的就是它的Replace函数可以消灭掉字符串中的所有指定文本,

比如对 CString str="He is doing something,something interesting,something that I don't know." ;

使用 str.Replace("th","");

结果就会消除掉 str中的所有 "th",用起来就像 php一样方便。可惜在STL里面没有这样的函数。)

 

CString除了比STL多点实用的函数,其他方面还是不能和标准库相比的。

最可恶的就是,一句 #include <afx.h>,就能使可执行文件从数百 KB 一跃至数 MB ,增大6、7倍。

恐怕,今后我会不用MFC了。

 

Windows编程,老老实实地用SDK吧。

0
0
分享到:
评论

相关推荐

    Unicode CString和 std::string 的完美转换,不会有64K限制

    Unicode CString和 std::string 的完美转换,不会有64K限制

    C ++:将MFC CString转换为std :: string

    在UNICODE中:CString str = L“ Test”; std :: wstring ws(str); std :: string s; s.assign(ws.begin(),ws.end());

    CString string char 之间的相互转换

    void CString_ansi_to_string(CString cstr,std::string& str) void CString_ansi_to_string_Ex(CString cstr,std::string& str) void CString_unicode_to_string(CString cstr,std::string& str) void CString_...

    Super string 库

    std::string CString_2_stdString(CString str); CString stdString_2_CString(std::string str); void CString_2_charP(CString str,char *q); //cstring change to ansi string //-------------判断字符串...

    std::vector排序详解

    一个详细全面的关于vector排序的例子。 涉及到 如何继承std::binary_function&lt;CString, CString, bool&gt;、如何重载operator().

    重载静态文本控件CStatic,支持背景贴图和透明,v1.2

    //输入:lfCharSet字体字符集,通常情况对于中文的Windows系统,也可以直接用DEFAULT_CHARSET表示默认字符集 // 英文字体可以用ANSI_CHARSET,简体中文字体可以用GB2312_CHARSET,繁体中文字体可以用CHINESEBIG5_...

    重载静态文本控件CStatic,支持背景贴图和透明,v1.1

    //输入:lfCharSet字体字符集,通常情况对于中文的Windows系统,也可以直接用DEFAULT_CHARSET表示默认字符集 // 英文字体可以用ANSI_CHARSET,简体中文字体可以用GB2312_CHARSET,繁体中文字体可以用CHINESEBIG5_...

    VC 使用StdString类代替CString类的方法和示例源码.rar

    VC 使用StdString类代替CString类的方法和示例源码,支持ansi、unicode。实际在一些项目中用了一段,感觉非常棒,特此推荐给大家。

    重载树控件CTreeCtrl,v1.1

    CString GetAutherEmail(); //功能:判断输入的节点句柄是否存在 //参数:[in] hItem 节点句柄 //返回:true节点存在,false节点不存在 bool FindItemExist(HTREEITEM hItem); //功能:设置一个节点字体颜色...

    super_CTime CTime的各种转换子程序库。

    super_CTime CTime的各种... CTime get_dateTime_from_string(std::string date, std::string time);//字符串不带分隔符 CTime get_dateTime_from_FGFstring(std::string date, std::string time); //字符串带分隔符

    StdString类代码,可完善替代CString字符串类.rar

    一个VC 中的StdString类源代码,可完善替代CString字符串类,支持ansi、unicode。实际在一些项目中用了一段,感觉非常棒,推荐给朋友们。

    wince开发用转换类

    static CString string_to_cstring(string text); static int string_to_int(string str); static string cstring_to_string(CString text); static void Gb2312ToUtf8(char* pstrOut, u32 dwOutLen, const char* ...

    StdString类代码,可完善替代CString字符串类

    一个VC++中的StdString类源代码,可完善替代CString字符串类,支持ansi、unicode。实际在一些项目中用了一段,感觉非常棒,推荐给朋友们。

    比CString更高效的字符串替换

    字符串的替换操作中,发现CString::Replace运行缓慢,也无法预期它的完成时间,所以编写了一个类似CString的类,效果不错!

    CString::Format函数详细介绍

    CString::Format函数详细介绍,这个文档详细介绍了CString类中对格式转换的基本介绍,尤其是在MFC中转换字符类型很有用处。

    一个跨平台的CString源码

    // FILE: StdString.h // AUTHOR: Joe O'Leary (with outside help noted in comments) // // If you find any bugs in this code, please let me know: // // jmoleary@earthlink.net // ...

    计算器 mfc 代码

    一、实验目的:构造一个类似Windows自带的计算器一样的简易计算器,能够连续进行加、减、乘、除四则整数运算,并能随时清除计算结果进行下一次计算。 二、具体实验步骤: 1、添加编辑框对应的变量m_Display 2、...

    简易图像处理软件

    std::string tempName=(LPCSTR)CString(fileName); const char* tmp=tempName.c_str(); if((pImg=cvLoadImage(tmp,1))==0) return; wImg=cvCreateImage(cvGetSize(pImg),pImg-&gt;depth,pImg-&gt;nChannels); cvCopy...

Global site tag (gtag.js) - Google Analytics