为啥测试结果 string 比 CString 快好多???

cjianwen 2012-05-08 04:48:56
// testCString.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "testCString.h"
#include <string>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;


void test1()
{
//CString s;
string s;
s = "123";
while(s.length() < 1024 * 1024 * 10)
{
s += s;
}
}

void testc()
{
CString s;
s = "123";
while(s.GetLength() < 1024 * 1024 * 10)
{
s += s;
}
}

void test()
{
for(int i = 1; i != 500; ++i)
{
test1();
//testc();
}
};


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
DWORD tick = GetTickCount();
test();
tick = GetTickCount() - tick;
CString s;
s.Format(_T("%d"), tick);
MessageBox(0, s.GetBuffer(), _T("test"), 0);

int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}



return nRetCode;
}
...全文
253 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
j8daxue 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
std::string 储存了字符串的长度(无需调用strlen,长字符串很慢), 内存分配优化每次是1.5倍(内存分配是非常慢的)
在大字符串拼接时, 这两点极大提高效率


CString 使用了引用计数, 单个对象复制时, 效率好, 内存消耗小

CString a,b,c;
...
a = b;
c = a;

这种情况 CString 的效率比std::stri……
[/Quote]
大体如此,不过CString貌似存了长度。
我觉得主要是慢在...Append->GetBuferr->PrepareWrite->PrepareWrite2看了最后一个函数你就知道为什么了。
其实倒是觉得CString设计的更有道理点。
sumos 2012-05-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
// testCString.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "testCString.h"
#include <string>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


/……
[/Quote]
从 设计 stl 和 MFC template 来看:
stl是牺牲空间,换取时间。
MFC是牺牲时间,换取空间。
cjianwen 2012-05-09
  • 打赏
  • 举报
回复
#include <iostream>
#include <hash_map>
#include <string>
#include "stdlib.h"
#include "afx.h"


using namespace std;
using namespace stdext;

typedef CString TString;
//typedef string TString;
typedef TString *PString;
typedef hash_map<int, PString> HashMap_IS;

int main()
{

char cs[20];
_itoa_s(100, cs, sizeof(cs), 10);
cout << cs << endl;

HashMap_IS hm;
PString ps = new TString();
hm.insert(HashMap_IS::value_type(1, ps));

DWORD tick = GetTickCount();

for(int i = 2; i != 1000000; ++i)
{
_itoa_s(i, cs, sizeof(cs), 10);
ps = new TString(cs);
hm.insert(HashMap_IS::value_type(i, ps));
}

tick = GetTickCount() - tick;
_tprintf(_T("insert time = %d\n"), tick);
_tscanf_s(_T("test:"), &tick);
return 0;

}


又测试了一下, CString 5578 毫秒, string 3500 左右
Eleven 2012-05-08
  • 打赏
  • 举报
回复
CString很强大
lgstudyvc 2012-05-08
  • 打赏
  • 举报
回复
这都能发现,用那上的?
CsdnJiessie 2012-05-08
  • 打赏
  • 举报
回复
就是MFC的问题。其实MFC没什么好的,又大又麻烦,呵呵,除了CString,把CString剥离出来就好了
gold_water 2012-05-08
  • 打赏
  • 举报
回复
CString是封装对字符串操作的类!是在MFC中使用,功能比较强,效率低点;
Yofoo 2012-05-08
  • 打赏
  • 举报
回复
std::string 储存了字符串的长度(无需调用strlen,长字符串很慢), 内存分配优化每次是1.5倍(内存分配是非常慢的)
在大字符串拼接时, 这两点极大提高效率


CString 使用了引用计数, 单个对象复制时, 效率好, 内存消耗小

CString a,b,c;
...
a = b;
c = a;

这种情况 CString 的效率比std::string好


综合来说新的std::string当然更好
zhanshen2891 2012-05-08
  • 打赏
  • 举报
回复
知道STL快就好了
ouyh12345 2012-05-08
  • 打赏
  • 举报
回复
string其实是char的动态数组,CString是字符串,用NULL结束符的
感觉stl的malloc是经过优化的
dahaiI0 2012-05-08
  • 打赏
  • 举报
回复
string数据多时没试过,CString数据量大,执行个 +=操作,CPU占用会比较高,而且调用像replace,find这类函数也会比较慢
cjianwen 2012-05-08
  • 打赏
  • 举报
回复

// testCString.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "testCString.h"
#include <string>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;


void test1()
{
//CString s;
string s;
s = "123";
while(s.length() < 1024 * 1024 * 10)
{
s += s;
}
}

void testc()
{
CString s;
s = "123";
while(s.GetLength() < 1024 * 1024 * 10)
{
s += s;
}
}

void test()
{
for(int i = 1; i != 500; ++i)
{
test1();
//testc();
}
};


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
DWORD tick = GetTickCount();
test();
tick = GetTickCount() - tick;
CString s;
s.Format(_T("%d"), tick);
MessageBox(0, s.GetBuffer(), _T("test"), 0);

int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}



return nRetCode;
}



再发一次代码 test1 和 testc 速度相差太多了

用 test1 比 testc 快一倍多。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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