/********************************************************************
file name : new.h
author : Clark/陈泽丹
created : 2011-11-29
*********************************************************************/
#include <iostream>
#include <Windows.h>
using namespace std;
void test1()
{
int* p = new int;
delete p;
p = NULL;
}
void test2()
{
int p;
}
void main()
{
int s, e;
s = GetTickCount();
for(int i=0; i<10000; i++)
test2();
e = GetTickCount();
cout<<"test2 cost: "<<e-s<<endl;
s = GetTickCount();
for(int i=0; i<10000; i++)
test1();
e = GetTickCount();
cout<<"test1 cost: "<<e-s<<endl;
system("pause");
}test2 cost: 0
test1 cost: 32
请按任意键继续. . .
本文通过测试C++中两种不同的内存管理方式(test1与test2),对比它们在大量循环执行下的性能差异,旨在揭示C++内存分配与释放的最佳实践。
1818

被折叠的 条评论
为什么被折叠?



