Here we compare the performance of C ++ Code Compiled by VC ++.
I used vc6.0 for testing.
I will not describe these usage cases.
I wrote a simple test code.
The test result is:
Malloc: 390
"New: 391"
Virtualalloc: 454
Heapalloc: 47
Obviously, heapalloc is the fastest allocation speed, followed by malloc. New is similar to malloc, and virtualalloc is the slowest (I told Xiaoqiang that it was the fastest in the past)
I tracked it.
New calls this code.
- Void * _ cdecl _ nh_malloc (
- Size_t nsize,
- Int nhflag
- )
- {
- Return _ nh_malloc_dbg (nsize, nhflag, _ normal_block, null, 0 );
- }
The malloc function is as follows:
- _ Cribd void * _ cdecl malloc (
- Size_t nsize
- )
- {
- Return _ nh_malloc_dbg (nsize, _ newmode, _ normal_block, null, 0 );
- }
Obviously, new and malloc eventually call the same _ nh_malloc_dbg, but new has an additional callback function call.
Continue and find that the final call is return heapalloc (_ crtheap, 0, size );
The truth is that
Virtualalloc cannot be tracked. If virtual memory is allocated, it may be slow.
Let's take a closer look at the book windows core programming!
Thank you! Welcome!
The test code is as follows:
- /*************************************** ***************************
- *
- * Copyright (c) 2008, xxxx
- * All Rights Reserved.
- *
- * File name: Main. cpp
- * Abstract: test the memory application speed.
- *
- * Version: 1.0
- * Author: Wu huiran
- * Completion date:
- *
- * Replaced version:
- * Original Author:
- * Completion date:
- *
- **************************************** **************************/
- # Include <iostream>
- # Include <windows. h>
- Using namespace STD;
- Int main (INT argc, char * argv [])
- {
- Int I = 0;
- DWORD dw1 = 0, dw2 = 0, dw3 = 0, dw4 = 0;
- DWORD dwstart = 0;
- DWORD dwend = 0;
- For (Int J = 0; j <10; j ++)
- {
- Dwstart =: gettickcount ();
- For (I = 0; I <20000; I ++)
- {
- Char * pdest1 = (char *) malloc (4096 );
- Free (pdest1 );
- }
- Dwend =: gettickcount ();
- Cout <"malloc 10000 times memory block size, time consumed" <dwend-dwstart <Endl;
- Dw1 + = dwend-dwstart;
- Dwstart =: gettickcount ();
- For (I = 0; I <20000; I ++)
- {
- Char * pdest2 = new char [4096];
- Delete pdest2;
- }
- Dwend =: gettickcount ();
- Cout <"10000 new 4096 memory blocks, time consumed" <dwend-dwstart <Endl;
- Dw2 + = dwend-dwstart;
- Dwstart =: gettickcount ();
- For (I = 0; I <20000; I ++)
- {
- Void * pmem =: virtualalloc (null, 4096, mem_reserve | mem_commit, page_readwrite );
- : Virtualfree (pmem, 0, mem_release );
- }
- Dwend =: gettickcount ();
- Cout <"virtualalloc 10000 times memory block size, time consumed" <dwend-dwstart <Endl;
- Dw3 + = dwend-dwstart;
- Handle hheap =: heapcreate (heap_no_serialize, 0, 0 );
- Dwstart =: gettickcount ();
- For (I = 0; I <20000; I ++)
- {
- Void * pmem2 =: heapalloc (hheap, heap_no_serialize, 4096 );
- : Heapfree (hheap, heap_no_serialize, pmem2 );
- }
- Dwend =: gettickcount ();
- Cout <"heapalloc 10000 memory blocks per 4096 times, time consumed" <dwend-dwstart <Endl;
- Dw4 + = dwend-dwstart;
- }
- Cout <"malloc:" <dw1 <Endl;
- Cout <"New:" <dw2 <Endl;
- Cout <"virtualalloc:" <dw3 <Endl;
- Cout <"heapalloc:" <dw4 <Endl;
- Return 0;
- }