64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
void* operator new(size_t size)
{
static int cnt;
cout << "new: " << ++cnt << endl;
return ::malloc(size);
}
void operator delete(void* p)
{
static int cnt;
cout << "delete: " << ++cnt << endl;
::free(p);
}
int main()
{
vector<int> v1(1);
int* p1 = new int(10);
vector<int> v2(1);
int* p2 = new int(8);
vector<int> v3(1);
vector<int> v4(1);
vector<int> v5(1);
delete p1;
delete p2;
return 0;
}
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
#define MAX_COUNT 1000000
int main()
{
vector<int> v;
DWORD t = GetTickCount();
for (int i = 0; i < MAX_COUNT; ++i)
{
int* p = v.get_allocator().allocate(100);
v.get_allocator().deallocate(p, 100);
}
std::cout << GetTickCount() - t << std::endl;
t = GetTickCount();
for (int i = 0; i < MAX_COUNT; ++i)
{
vector<int> vt(100);
}
std::cout << GetTickCount() - t << std::endl;
t = GetTickCount();
for (int i = 0; i < MAX_COUNT; ++i)
{
int* p = ::new int[100];
delete[] p;
}
std::cout << GetTickCount() - t << std::endl;
return 0;
}