1 #include <iostream> 2 #include <time.h> 3 #include <stdlib.h> 4 5 using namespace std; 6 7 #define LOOP_COUNT 500000 8 int main() 9 { 10 char buf[10000] = {0}; 11 12 int* obj = NULL; 13 14 int i = LOOP_COUNT; 15 16 clock_t start = clock(); 17 while(i--) 18 { 19 obj = new (buf) int [1000]; 20 21 obj = NULL; 22 } 23 clock_t end = clock(); 24 25 cout << "Placement new : " << end - start << " ms" << endl; 26 27 28 i = LOOP_COUNT; 29 start = clock(); 30 while(i--) 31 { 32 obj = new int [1000]; 33 //delete obj; 34 obj = NULL; 35 } 36 end = clock(); 37 38 cout << "Normal new : " << end - start << " ms" << endl; 39 }
Test case 1. new 操作不delete内存
//////////////// output
Placement new : 0
Normal new : 3234
Test case 2. new 操作, delete内存
//////////////// output
Placement new : 0
Normal new : 1594
时间: 2024-10-08 10:33:33