流量统计:
Rixu Blog (日需博客)
日需博客,每日必需来踩踩哦..
posts - 108,comments - 54,trackbacks - 0

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

我用的是VC6.0测试的

就不介绍这几个的用法了

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

测试结果是:

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

很明显的是HeapAlloc分配速度最快,malloc次之,newmalloc差不多,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. }
  7.  

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

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

基本上真相大白了

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

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

欢迎指正!欢迎交流!

测试代码如下:

  1. /******************************************************************
  2. *
  3. * Copyright (c) 2008, xxxx
  4. * All rights reserved.
  5. *
  6. 文件名称:main.cpp
  7.     要: 测试申请内存的速度
  8. *
  9. 当前版本:1.0
  10.      者:吴会然
  11. 完成日期:2008-11-30
  12. *
  13. 取代版本:
  14.    作者:
  15. 完成日期:
  16. *
  17. ******************************************************************/
  18.  
  19. #include <iostream>
  20. #include <windows.h>
  21. using namespace std;
  22.  
  23. int main( int argc, char *argv[] )
  24. {
  25.     int i = 0;
  26.     DWORD dw1 = 0, dw2 = 0, dw3 = 0, dw4 = 0;
  27.     DWORD dwStart = 0;
  28.     DWORD dwEnd = 0;
  29.     forint j = 0; j < 10; j++ )
  30.      {
  31.          dwStart = ::GetTickCount();
  32.         for( i = 0; i < 20000; i++ )
  33.          {
  34.             char *pDest1 = (char *)malloc(4096);
  35.              free( pDest1 );
  36.     
  37.          }
  38.          dwEnd = ::GetTickCount();
  39.          cout << "malloc 100004096大小的内存块,耗时" << dwEnd - dwStart << endl;
  40.          dw1 += dwEnd - dwStart;
  41.  
  42.          dwStart = ::GetTickCount();
  43.         for( i = 0; i < 20000; i++ )
  44.          {
  45.             char *pDest2 = new char[4096];
  46.             delete pDest2;
  47.     
  48.          }
  49.          dwEnd = ::GetTickCount();
  50.          cout << "new 100004096大小的内存块,耗时" << dwEnd - dwStart << endl;
  51.          dw2 += dwEnd - dwStart;
  52.  
  53.          dwStart = ::GetTickCount();
  54.         for( i = 0; i < 20000; i++ )
  55.          {
  56.             void* pMem = ::VirtualAlloc(NULL, 4096,   MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
  57.              ::VirtualFree(pMem, 0, MEM_RELEASE);
  58.          }
  59.          dwEnd = ::GetTickCount();
  60.          cout << "VirtualAlloc 100004096大小的内存块,耗时" << dwEnd - dwStart << endl;
  61.          dw3 += dwEnd - dwStart;
  62.  
  63.         HANDLE hHeap = ::HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
  64.          dwStart = ::GetTickCount();
  65.         for( i = 0; i < 20000; i++ )
  66.          {
  67.             void* pMem2 = ::HeapAlloc(hHeap, HEAP_NO_SERIALIZE, 4096 );
  68.              ::HeapFree(hHeap, HEAP_NO_SERIALIZE, pMem2);
  69.  
  70.          }
  71.          dwEnd = ::GetTickCount();
  72.          cout << "HeapAlloc 100004096大小的内存块,耗时" << dwEnd - dwStart << endl;
  73.          dw4 += dwEnd - dwStart;
  74.  
  75.      }
  76.  
  77.      cout << "malloc:" << dw1 << endl;
  78.      cout << "new:" << dw2 << endl;   
  79.      cout << "VirtualAlloc:" << dw3 << endl;   
  80.      cout << "HeapAlloc:" << dw4

 

 

 

====================================

1 把分配的内存空间改为4M甚至更高(循环次数减少)试试,结果截然不同。处理M级的大文件时,用VirtualAlloc效率高

2 博主的测试需要考究,系统在创建线程时就已经预先在线程的堆栈段中提交了两个页面。 按照我的观点new和malloc只要消耗的内存没有超过页面大小就不会实际的进行存储器的保留与提交。二者的操作不在一个层面上。

3 在分配大于一个页面数据的时候 virtualAlloc才有意义

4 virtualAlloc是操作系统提供的最根本的内存分配接口。HeapAlloc预先使用virtualAlloc申请了大块的内存,并根据优化算法组织了用于内存管理的数据结构,主要是对小内存分配的优化 new和malloc是语言层面接口,由于HeapAlloc已经有了优化,所以vc中的malloc并没有使用更多的优化算法,直接转入 HeapAlloc。

Logo
作者:Gezidan
出处:http://www.rixu.net    
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2011-08-15 09:19 日需博客 阅读(1309) 评论(2)  编辑 收藏 引用 所属分类: C C++Windows技术文章转载

FeedBack:
# re: malloc,new,VirtualAlloc,HeapAlloc性能(速度)比较[未登录]
2011-08-23 13:43 | Chipset
这种测试根本测不出真实速度。主要原因是刚分配一块内存就放掉,再分配还是刚才释放的那块内存,具体应用中则不是这样的,再者不应该始终分配一样的块大小。

应该这样测试:分配块大小随机,把返回的指针存入一个数组,然后释放也随机进行。这样才能看出它们的"真实"速度。常采用的做法是使用脚本测试程序,因为脚本狂吃内存,使用不同的内存管理函数或API比较一下脚本的执行速度。

其实只衡量速度是不科学的,还应该看下碎片率,峰值内存,页面错误,还有CPU缓存丢失率等指标。  回复  更多评论
  
# re: malloc,new,VirtualAlloc,HeapAlloc性能(速度)比较
2011-11-18 11:00 | 星绽紫辉
@Chipset
When you allocate memory with new, the memory is allocated in the virtual memory or the physical memory. Where the memory is allocated is unknown, the memory can be allocated between two pages. This means that we load too much memory into the physical memory when we access a certain data (if we use new). Furthermore, you do not know if the allocated memory is in physical memory or in virtual, and also you can not tell the system when "writing back" to hard disk is unnecessary (if we don’t care of the data in memory anymore). But be aware!! Any new allocation using VirtualAlloc* will always be rounded up to 64 KB (page file size) boundary so that if you allocate a new VAS region bound to the physical memory, the OS will consume an amount of physical memory rounded up to the page size, and will consume the VAS of the process rounded up to 64 KB boundary. Using VirtualAlloc can be difficult: new and malloc use virualAlloc internally, but every time you allocate memory with new/delete, a lot of other computation is done, and you do not have the control to put your data (data related to each other) nicely inside the same page (without overlapping two pages). However, heaps are best for managing large numbers of small objects, and I shall change the source code so it only uses new/delete because of code cleanness. I have found that the performance gain is too small relative when compared to the complexity of the source code.  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理