CString +运算符或格式

时间:2017-06-22 04:29:45

标签: visual-c++

我有一个这样的示例代码:

CString A = _T("abc");
CString B = _T("xyz");
CString res;

现在要连接上面这两个字符串,我应该选择哪一个:

res = A + _T(" ") + B;

res.Format(_T("%s %s"), A, B);

1 个答案:

答案 0 :(得分:0)

制作样本函数并运行波纹管代码

CString str1 = _T("abc");
CString str2 = _T("xyz");
CString strRes;
DWORD dwTickCount = GetTickCount();
const int LoopCount = 1000000;
for(int nIdx = 0; nIdx < LoopCount; nIdx++)
{
    strRes = str1 + _T(" ") + str2;
}
dwTickCount = GetTickCount() - dwTickCount;

dwTickCount的值是2656.但在检查第二种情况时,

CString str1 = _T("abc");
CString str2 = _T("xyz");
CString strRes;
DWORD dwTickCount = GetTickCount();
const int LoopCount = 1000000;
for(int nIdx = 0; nIdx < LoopCount; nIdx++)
{
    strRes.Format(_T("%s %s"), str1, str2);
}
dwTickCount = GetTickCount() - dwTickCount;

dwTickCount的值仅为453。 因此,我可以说,格式化方法的速度提高了约6倍。