• <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>
            流量統(tǒng)計(jì):
            Rixu Blog (日需博客)
            日需博客,每日必需來(lái)踩踩哦..
            posts - 108,comments - 54,trackbacks - 0

            這里比較的VC++編譯的C++代碼中的性能

            我用的是VC6.0測(cè)試的

            就不介紹這幾個(gè)的用法了

            我寫(xiě)了一段簡(jiǎn)單的測(cè)試代碼

            測(cè)試結(jié)果是:

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

            很明顯的是HeapAlloc分配速度最快,malloc次之,newmalloc差不多,VirtualAlloc最慢了(以前小強(qiáng)跟我說(shuō)這個(gè)最快)

            我有跟蹤了一下

            new調(diào)用了這段代碼

            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函數(shù)是這樣的:

            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最終調(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à),有可能會(huì)慢吧。

            回頭再認(rèn)真看看《Windows核心編程》這本書(shū)!

            歡迎指正!歡迎交流!

            測(cè)試代碼如下:

            1. /******************************************************************
            2. *
            3. * Copyright (c) 2008, xxxx
            4. * All rights reserved.
            5. *
            6. 文件名稱(chēng):main.cpp
            7.     要: 測(cè)試申請(qǐng)內(nèi)存的速度
            8. *
            9. 當(dāng)前版本:1.0
            10.      者:吳會(huì)然
            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大小的內(nèi)存塊,耗時(shí)" << 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大小的內(nèi)存塊,耗時(shí)" << 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大小的內(nèi)存塊,耗時(shí)" << 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大小的內(nèi)存塊,耗時(shí)" << 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 把分配的內(nèi)存空間改為4M甚至更高(循環(huán)次數(shù)減少)試試,結(jié)果截然不同。處理M級(jí)的大文件時(shí),用VirtualAlloc效率高

            2 博主的測(cè)試需要考究,系統(tǒng)在創(chuàng)建線(xiàn)程時(shí)就已經(jīng)預(yù)先在線(xiàn)程的堆棧段中提交了兩個(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。

            Logo
            作者:Gezidan
            出處:http://www.rixu.net    
            本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
            posted on 2011-08-15 09:19 日需博客 閱讀(1370) 評(píng)論(2)  編輯 收藏 引用 所屬分類(lèi): C C++Windows技術(shù)文章轉(zhuǎn)載

            FeedBack:
            # re: malloc,new,VirtualAlloc,HeapAlloc性能(速度)比較[未登錄](méi)
            2011-08-23 13:43 | Chipset
            這種測(cè)試根本測(cè)不出真實(shí)速度。主要原因是剛分配一塊內(nèi)存就放掉,再分配還是剛才釋放的那塊內(nèi)存,具體應(yīng)用中則不是這樣的,再者不應(yīng)該始終分配一樣的塊大小。

            應(yīng)該這樣測(cè)試:分配塊大小隨機(jī),把返回的指針存入一個(gè)數(shù)組,然后釋放也隨機(jī)進(jìn)行。這樣才能看出它們的"真實(shí)"速度。常采用的做法是使用腳本測(cè)試程序,因?yàn)槟_本狂吃?xún)?nèi)存,使用不同的內(nèi)存管理函數(shù)或API比較一下腳本的執(zhí)行速度。

            其實(shí)只衡量速度是不科學(xué)的,還應(yīng)該看下碎片率,峰值內(nèi)存,頁(yè)面錯(cuò)誤,還有CPU緩存丟失率等指標(biāo)。  回復(fù)  更多評(píng)論
              
            # 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.  回復(fù)  更多評(píng)論
              
            久久精品国产亚洲一区二区| 久久天天躁夜夜躁狠狠| 久久国产精品99国产精| 久久久久久精品久久久久| 国产色综合久久无码有码| 少妇高潮惨叫久久久久久 | 久久e热在这里只有国产中文精品99 | 亚洲AV无码久久精品蜜桃| 久久久噜噜噜久久熟女AA片| 色综合合久久天天综合绕视看| 久久精品国产一区| 少妇人妻综合久久中文字幕| 国产欧美久久久精品| 中文字幕精品无码久久久久久3D日动漫 | 精品一二三区久久aaa片| 久久婷婷国产麻豆91天堂| 性高朝久久久久久久久久| 99久久99久久| 亚洲精品国精品久久99热一| 婷婷综合久久狠狠色99h| 久久久久亚洲av综合波多野结衣 | 色青青草原桃花久久综合| 久久精品国产影库免费看 | 久久亚洲精品无码观看不卡| 99久久超碰中文字幕伊人| 亚洲AⅤ优女AV综合久久久| 国产精品一久久香蕉国产线看| 欧美色综合久久久久久| 91久久九九无码成人网站| 亚洲午夜久久久久久久久久| 人人狠狠综合久久亚洲| 2020最新久久久视精品爱| 国内精品久久久久伊人av| 亚洲人成精品久久久久| 免费精品久久天干天干| 亚洲国产日韩欧美综合久久| 精品国产乱码久久久久久浪潮| 色综合色天天久久婷婷基地| 久久99国产精品99久久| 97久久超碰国产精品旧版| 久久精品国产亚洲AV无码麻豆|