关于MFC的new操作符分配内存的性能问题
以下有两个程序一个是win32版本,一个是MFC版本。在我的机器上跑(release版本),win32的为343,MFC的为468。我的问题是为什么MFC的new会慢这么多。
Win32版本:
#include <ctime>
#include <iostream>
using namespace std;
void Test()
{
clock_t elapsed, tick = clock();
for(long i = 0; i < 1000000; i++)
{
char *data = new char[1024];
delete[] data;
}
elapsed = clock() - tick;
cout<<"time elapsed is "<<elapsed<<" milliseconds"<<endl;
}
int main(int argc, char* argv[])
{
Test();
return 0;
}
MFC版本:
#include "stdafx.h"
CWinApp theApp;
using namespace std;
void Test()
{
clock_t elapsed, tick = clock();
#undef new
for(long i = 0; i < 1000000; i++)
{
char *data = new char[1024];
delete[] data;
}
elapsed = clock() - tick;
cout<<"time elapsed is "<<elapsed<<" milliseconds"<<endl;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
Test();
return nRetCode;
}