這里比較的VC++編譯的C++代碼中的性能
我用的是VC6.0測(cè)試的
就不介紹這幾個(gè)的用法了
我寫(xiě)了一段簡(jiǎn)單的測(cè)試代碼
測(cè)試結(jié)果是:
malloc:390
new:391
VirtualAlloc:454
HeapAlloc:47
很明顯的是HeapAlloc分配速度最快,malloc次之,new和malloc差不多,VirtualAlloc最慢了(以前小強(qiáng)跟我說(shuō)這個(gè)最快)
我有跟蹤了一下
new調(diào)用了這段代碼
- void * __cdecl _nh_malloc (
- size_t nSize,
- int nhFlag
- )
- {
- return _nh_malloc_dbg(nSize, nhFlag, _NORMAL_BLOCK, NULL, 0);
- }
malloc函數(shù)是這樣的:
- _CRTIMP void * __cdecl malloc (
- size_t nSize
- )
- {
- return _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);
- }
-
很明顯,new和malloc最終調(diào)用相同的_nh_malloc_dbg,只是new多了一次函數(shù)調(diào)用
再繼續(xù)跟下去,發(fā)現(xiàn)最終調(diào)用的是return HeapAlloc(_crtheap, 0, size);
基本上真相大白了
VirtualAlloc跟蹤不進(jìn)去,如果說(shuō)分配的是虛擬內(nèi)存的話,有可能會(huì)慢吧。
回頭再認(rèn)真看看《Windows核心編程》這本書(shū)!
歡迎指正!歡迎交流!
測(cè)試代碼如下:
- /******************************************************************
- *
- * Copyright (c) 2008, xxxx
- * All rights reserved.
- *
- * 文件名稱(chēng):main.cpp
- * 摘 要: 測(cè)試申請(qǐng)內(nèi)存的速度
- *
- * 當(dāng)前版本:1.0
- * 作 者:吳會(huì)然
- * 完成日期:2008-11-30
- *
- * 取代版本:
- * 原 作者:
- * 完成日期:
- *
- ******************************************************************/
-
- #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次4096大小的內(nèi)存塊,耗時(shí)" << dwEnd - dwStart << endl;
- dw1 += dwEnd - dwStart;
-
- dwStart = ::GetTickCount();
- for( i = 0; i < 20000; i++ )
- {
- char *pDest2 = new char[4096];
- delete pDest2;
-
- }
- dwEnd = ::GetTickCount();
- cout << "new 10000次4096大小的內(nèi)存塊,耗時(shí)" << 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次4096大小的內(nèi)存塊,耗時(shí)" << 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次4096大小的內(nèi)存塊,耗時(shí)" << dwEnd - dwStart << endl;
- dw4 += dwEnd - dwStart;
-
- }
-
- cout << "malloc:" << dw1 << endl;
- cout << "new:" << dw2 << endl;
- cout << "VirtualAlloc:" << dw3 << endl;
- cout << "HeapAlloc:" << dw4
====================================
轉(zhuǎn)】李瑋劍 評(píng)論
1 把分配的內(nèi)存空間改為4M甚至更高(循環(huán)次數(shù)減少)試試,結(jié)果截然不同。處理M級(jí)的大文件時(shí),用VirtualAlloc效率高
2 博主的測(cè)試需要考究,系統(tǒng)在創(chuàng)建線程時(shí)就已經(jīng)預(yù)先在線程的堆棧段中提交了兩個(gè)頁(yè)面。 按照我的觀點(diǎn)new和malloc只要消耗的內(nèi)存沒(méi)有超過(guò)頁(yè)面大小就不會(huì)實(shí)際的進(jìn)行存儲(chǔ)器的保留與提交。二者的操作不在一個(gè)層面上。
3 在分配大于一個(gè)頁(yè)面數(shù)據(jù)的時(shí)候 virtualAlloc才有意義
4 virtualAlloc是操作系統(tǒng)提供的最根本的內(nèi)存分配接口。HeapAlloc預(yù)先使用virtualAlloc申請(qǐng)了大塊的內(nèi)存,并根據(jù)優(yōu)化算法組織了用于內(nèi)存管理的數(shù)據(jù)結(jié)構(gòu),主要是對(duì)小內(nèi)存分配的優(yōu)化 new和malloc是語(yǔ)言層面接口,由于HeapAlloc已經(jīng)有了優(yōu)化,所以vc中的malloc并沒(méi)有使用更多的優(yōu)化算法,直接轉(zhuǎn)入 HeapAlloc。