Test the more-efficient tive-c ++ sequence 1 new and delete

Source: Internet
Author: User
More-effective tive-c ++ sequence 1 new and delete test 1. Allocate objects in heap memory and use your own method to allocate heap memory.

Widget * pWidget = new Widget ():
1. Call operator new to allocate memory
2. Call the constructor for the objects in the memory.
3. Convert to Widget * and assign it to pWidget.
Therefore, you can rewrite operator new and delete to determine the memory allocation mode.

The following code calls malloc in operator new and free in operator delete.
Of course, you can even use the memory pool. operator new calls GetMemory and operator delete calls ReturnMemory.
In this way, you can personalize your memory allocation methods.

# Include <iostream> using namespace std;/* new operator calls the constructor of operator new and Widget to reload operator new and delete to customize its own behavior. */# Define TRACE_FUCTION_AND_LINE (fmt ,...) printf ("[% 20 s: % 4d]" fmt "\ n" ,__ FUNCTION __, _ LINE __, ##__ VA_ARGS _) class Widget {public: widget () {TRACE_FUCTION_AND_LINE ();} void * operator new (size_t nSize) {void * p = malloc (sizeof (Widget )); TRACE_FUCTION_AND_LINE ("malloc p = % 08 p", p); return p;} void operator delete (void * memory) {TRACE_FUCTION_AND_LINE ("free p = % 08 p ", memory); free (memory );}~ Widget () {TRACE_FUCTION_AND_LINE () ;}}; int main () {Widget * pWidget = new Widget (); TRACE_FUCTION_AND_LINE ("begin to delete"); delete pWidget; return 0 ;}

2. We plan to allocate objects on the allocated memory using the placement new method.

In the following code main function, a stack memory buf is allocated first and then assigned to the memory pointer.
Call the new (memory) Widget (100) to pass the memory pointer to the operator new method.
Since operator new is designed to allocate memory, there is now a stack memory which can be used directly, so return buffer directly;
Then, the constructor of the Widget is called to initialize the object. After printing the log, you need to execute the destructor of the called object.
If you do not call the destructor, the object's destructor will not be called.
Maybe you will ask, why not call delete pw to release the memory? You can try it. Because operator delete uses the delete mode to delete memory by default, the delete is actually
Stack memory, which can cause problems immediately.

The Code Annotated below the main function is just a simple test of new, operator new, and malloc memory allocation and release methods. You just want to see which one is more efficient.
The test examples are incomplete. The allocation and release of general memory usage schemes are random. This example is not. It cannot accurately reflect the actual data, but at least it indicates that malloc seems faster.

# Include <iostream> # include <time. h> using namespace std;/* new operator calls the constructor of operator new and Widget to overload operator new and delete to implement placement new */# define TRACE_FUCTION_AND_LINE (fmt ,...) printf ("[% 20 s: % 4d]" fmt "\ n" ,__ FUNCTION __, _ LINE __, ##__ VA_ARGS _) class Widget {public: widget (int nHight = 0): m_nHight (nHight) {TRACE_FUCTION_AND_LINE ();} void * operator new (size_t nSize, void * buffer) {TRACE_FUCTION_AND_LINE ("m Alloc p = % 08 p ", buffer); return buffer ;}~ Widget () {TRACE_FUCTION_AND_LINE ();} void Log () {TRACE_FUCTION_AND_LINE ("My Log ------ % d", m_nHight);} int m_nHight;}; int main () {char buf [sizeof (Widget)]; // allocate the stack memory void * memory = buf; // allocate the widget memory, actually, the stack memory is TRACE_FUCTION_AND_LINE ("memory = % 08 p", memory); Widget * pw = new (memory) Widget (100 ); // placement new transfers the memory pointer to operator newpw-> Log (); pw-> ~ Widget (); // actively call the Destructor/* double start, end; while (1) {start = clock (); for (int I = 0; I <500; ++ I) {for (int j = 0; j <10000; ++ j) {void * pMemory = operator new (10000); operator delete (pMemory );}} end = clock (); TRACE_FUCTION_AND_LINE ("operator new takes %. 2f seconds ", (end-start)/CLOCKS_PER_SEC); start = clock (); for (int I = 0; I <500; ++ I) {for (int j = 0; j <10000; ++ j) {void * pMemory = new char [10000]; delete [] (pMemory );}} end = clock (); TRACE_FUCTION_AND_LINE ("new char takes %. 2f seconds ", (end-start)/CLOCKS_PER_SEC); start = clock (); for (int I = 0; I <500; ++ I) {for (int j = 0; j <10000; ++ j) {void * pMemory = malloc (10000); free (pMemory) ;}} end = clock (); TRACE_FUCTION_AND_LINE ("malloc takes %. 2f seconds ", (end-start)/CLOCKS_PER_SEC); TRACE_FUCTION_AND_LINE (" \ n ");} */return 0 ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.