malloc,new,VirtualAlloc,HeapAlloc性能(速度)比较

来源:互联网 发布:cad怎么添加网络打印机 编辑:程序博客网 时间:2024/05/01 22:26

这里比较的VC++编译的C++代码中的性能

我用的是VC6.0测试的

就不介绍这几个的用法了

 

我写了一段简单的测试代码

测试结果是:

malloc:390
new:391
VirtualAlloc:454
HeapAlloc:47

很明显的是HeapAlloc分配速度最快,malloc次之,new和malloc差不多,VirtualAlloc最慢了(以前小强跟我说这个最快)

我有跟踪了一下

new调用了这段代码

 

  1. void __cdecl _nh_malloc (
  2.         size_t nSize,
  3.         int nhFlag
  4.         )
  5. {
  6.         return _nh_malloc_dbg(nSize, nhFlag, _NORMAL_BLOCK, NULL, 0);
  7. }

malloc函数是这样的:

  1. _CRTIMP void __cdecl malloc (
  2.         size_t nSize
  3.         )
  4. {
  5.         return _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);
  6. }

很明显,new和malloc最终调用相同的_nh_malloc_dbg,只是new多了一次函数调用

再继续跟下去,发现最终调用的是return HeapAlloc(_crtheap, 0, size);

基本上真相大白了

VirtualAlloc跟踪不进去,如果说分配的是虚拟内存的话,有可能会慢吧。

回头再认真看看《Windows核心编程》这本书!

#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 << endl;   
    return 0;
}

欢迎指正!欢迎交流!

原创粉丝点击