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