帮忙看下这段代码哪部分花费时间

czbever 2009-03-25 08:40:21
我是在ARM上执行的,平均花费时间50秒,大家知道怎么优化吗?可以考虑修改代码或者用ARM汇编,但是ARM汇编我不懂,看有没有更好的办法呢?


CString CMyConnection::ReadUTF()
{
if(!m_pBufferRead)
{
return L"";
}
DWORD dwBegin =GetTickCount();

int Size = 0;
Size |= m_pBufferRead[m_ReadIndex++] << 8;
Size |= m_pBufferRead[m_ReadIndex++];
char *szUTF8 = new char[Size + 1];
memset(szUTF8,0,sizeof(char)*(Size + 1));
int i=0;
while(i<Size)
{
szUTF8[i] = m_pBufferRead[i+m_ReadIndex];
i++;
}
szUTF8[i] = '\0';
m_ReadIndex += Size;
DWORD dwNum = MultiByteToWideChar (CP_UTF8, 0, szUTF8, -1, NULL, 0);
//分配相应的数组空间:
TCHAR *szText= new TCHAR[dwNum];
memset(szText,0,sizeof(TCHAR)*dwNum);
if(!szText)
{
delete []szText;
wprintf(L"接受数据内存分配失败");
return FALSE;
}
//UTF-8转UTF-16:
MultiByteToWideChar (CP_UTF8, 0, szUTF8, -1, szText, dwNum);
szText[dwNum - 1] = '\0';
CString strRec = L"";
strRec += szText;

CString strText = L"";
strText.Format(L"接收到:%s\n",strRec);

wprintf(strText);
//释放占用的内存:

DWORD dwEnd = GetTickCount();
CString strTime = L"";
strTime.Format(L"\nReadUTF 花费时间:%ld 毫秒\n",dwEnd-dwBegin );
wprintf(strTime);

if(szText)
{
delete []szText;
}
if(szUTF8)
{
delete []szUTF8;
}
return strRec;
}
...全文
165 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
wprintf(strTimeMulti);
///////////////////////////////////////////////////////////

DWORD dwEnd = GetTickCount();
CString strTime = L"";

只能看出这段花了28秒
czbever 2009-03-25
  • 打赏
  • 举报
回复
ReadUTF 花费时间:83 毫秒
接收到:山东


Format 花费时间:55 毫秒
czbever 2009-03-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ForestDB 的回复:]
看架势应该有profile工具可以使用的。
[/Quote]

profile工具?哪个工具?
czbever 2009-03-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hairetz 的回复:]
引用 3 楼 czbever 的回复:
CString strText = L"";
strText.Format(L"接收到:%s\n",szText);
wprintf(strText);

居然是这句花时间 好奇怪


你怎么知道的呢?
[/Quote]


CString CMyConnection::ReadUTF()
{
if(!m_pBufferRead)
{
return L"";
}
DWORD dwBegin =GetTickCount();
int Size = 0;
Size |= m_pBufferRead[m_ReadIndex++] << 8;
Size |= m_pBufferRead[m_ReadIndex++];

char *szUTF8 = new char[Size + 1];
memset(szUTF8,0,sizeof(char)*(Size + 1));
int i=0;
while(i<Size)
{
szUTF8[i] = m_pBufferRead[i+m_ReadIndex];
i++;
}
szUTF8[i] = '\0';
m_ReadIndex += Size;


DWORD dwNum = MultiByteToWideChar (CP_UTF8, 0, szUTF8, -1, NULL, 0);
//分配相应的数组空间:


/////////////
TCHAR *szText= new TCHAR[dwNum];
memset(szText,0,sizeof(TCHAR)*dwNum);
if(!szText)
{
delete []szText;
wprintf(L"接受数据内存分配失败");
return FALSE;
}
//UTF-8转UTF-16:

MultiByteToWideChar (CP_UTF8, 0, szUTF8, -1, szText, dwNum);

szText[dwNum - 1] = '\0';
///////////////////////////////
DWORD dwBeginMulti =GetTickCount();

CString strText = L"";
strText.Format(L"接收到:%s\n",szText);
wprintf(strText);


DWORD dwEndMulti = GetTickCount();
CString strTimeMulti = L"";
strTimeMulti.Format(L"\n Format 花费时间:%ld 毫秒\n",dwEndMulti-dwBeginMulti);
wprintf(strTimeMulti);
///////////////////////////////////////////////////////////

DWORD dwEnd = GetTickCount();
CString strTime = L"";
strTime.Format(L"\nReadUTF 花费时间:%ld 毫秒\n",dwEnd-dwBegin );
wprintf(strTime);

//释放占用的内存:
if(szText)
{
delete []szText;
}
if(szUTF8)
{
delete []szUTF8;
}
return strText;
}
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 czbever 的回复:]
CString strText = L"";
strText.Format(L"接收到:%s\n",szText);
wprintf(strText);

居然是这句花时间 好奇怪
[/Quote]

你怎么知道的呢?
ForestDB 2009-03-25
  • 打赏
  • 举报
回复
看架势应该有profile工具可以使用的。
czbever 2009-03-25
  • 打赏
  • 举报
回复
CString strText = L"";
strText.Format(L"接收到:%s\n",szText);
wprintf(strText);

居然是这句花时间 好奇怪
  • 打赏
  • 举报
回复
你测试一下new所需要的时间是多少.
我觉得时间消耗在new和MultiByteToWideChar时间上面了


要是觉得 utf-8到unicode转换慢的话 可以自己写转换算法.转换规则固定.也比较简单
ysysbaobei 2009-03-25
  • 打赏
  • 举报
回复

65,211

社区成员

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

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