• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            流量統計:
            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 日需博客 閱讀(1370) 評論(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.  回復  更多評論
              
            久久99精品国产| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久久久免费看成人影片| 久久久久99精品成人片欧美| 国产成人久久激情91| 午夜精品久久久久久久无码| 亚洲午夜久久久影院伊人| 久久亚洲私人国产精品vA| 久久天天躁狠狠躁夜夜2020老熟妇| 久久久久久久97| 老司机午夜网站国内精品久久久久久久久| 久久久久亚洲av毛片大| 亚洲色欲久久久综合网| 国产午夜精品久久久久九九| 久久久久久av无码免费看大片 | 亚洲色欲久久久综合网| 国产叼嘿久久精品久久| 久久久久亚洲AV无码观看| 国产成人99久久亚洲综合精品| 国内精品久久久久久久久电影网 | 2021最新久久久视精品爱| 91精品日韩人妻无码久久不卡| 99精品久久精品一区二区| 久久精品男人影院| 日韩精品久久无码人妻中文字幕| 久久成人精品| 亚洲综合精品香蕉久久网97| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 久久久国产视频| 国产精品久久久久a影院| 亚洲国产精品久久66| 欧美va久久久噜噜噜久久| 狠狠色丁香婷婷久久综合| 青青热久久国产久精品| 国产亚洲美女精品久久久| 青草影院天堂男人久久| 办公室久久精品| segui久久国产精品| 99久久国产主播综合精品| 亚洲精品国产成人99久久| 亚洲国产精久久久久久久|