C++ 效能剖析 (三):Heap Object對比 Stack (auto) Object,heapstack_PHP教程

來源:互聯網
上載者:User

C++ 效能剖析 (三):Heap Object對比 Stack (auto) Object,heapstack


通常認為,效能的改進是90 ~ 10 規則, 即10%的代碼要對90%的效能問題負責。做過大型軟體工程的程式員一般都知道這個概念。

然而對於軟體工程師來說,有些效能問題是不可原諒的,無論它們屬於10%或是90%,都是“必須”改進的。這裡就講講其中的一個問題:用heap還是用stack的問題。

Java, C#,和JavaScript的程式員一般都不用管自己建立的object是在heap裡還是在stack裡,因為對於這些語言,object 只能“生活在”heap裡。這無疑對於程式員來說簡單了許多。但是對於C++程式員來說,你可以選擇三處來建立object:

  • 程式的data section
  • 工作堆棧
  • Heap

Object 因該生活在哪裡?這個問題必須由應用的屬性來決定,有時是沒有選擇的,比如對於動態產生的全程變數,只有活在heap裡,別無它途。

然而,一旦我們有選擇,比如臨時的,作為複雜資料的載體的object,答案是清楚的:應該首選stack. 比如下面簡單的例子:

// heap vs stack test

double HeapVsStack(bool heap, int loop, int &result)

{

if (heap)

{

clock_t begin = clock();

for(int i = 0; i < loop; ++i)

{

intPair *p = new intPair(1,2);

result += p->ip1 + p->ip2;

delete p;

}

clock_t end = clock();

return double(end - begin) / CLOCKS_PER_SEC;

}

else

{

clock_t begin = clock();

for(int i = 0; i < loop; ++i)

{

intPair p = intPair(1,2);

result += p.ip1 + p.ip2;

}

clock_t end = clock();

return double(end - begin) / CLOCKS_PER_SEC;

}

}

程式中黑體放大的部分是要測量的“應用邏輯”,上方在heap中建立了一個intPair,用完後delete掉。下方在stack裡定義一個同樣的auto變數,用完後無須care.

對這個程式作下列簡單測試調用:

int result = 0;

printf("Heap time: %f \n", HeapVsStack(true, 100000, result));

printf("Stack time: %f \n", HeapVsStack(false, 100000, result));

我不得不調用100000次,原因是它們的耗時差別實在太大了:stack 的用例不到10000次以上都顯示0ms.

測試結果,heap用了300ms, stack用了5ms, 相差60倍。

結論:

1) 如果應用邏輯容許,用 stack-based auto 變數,千萬不用 heap 變數.

2) 如果需要大量用heap,建議用std::vector來當作自己的 heap 簡單管理器用。避免直接地,大量地用heap來建立 ad-hoc object.

3) 有些臨時計算用的class可以考慮禁止在heap中產生,見http://stackoverflow.com/questions/1941517/explicitly-disallow-heap-allocation-in-c 文章。

2014-8-23 西雅圖


Heap memory與Stack memory他們的不同是什?

引用:www.blogjava.net/...9.aspx
上午看某文章時候涉及緩衝區溢位的問題,談到C的棧和堆,有所不懂於是baidu了一下發現論壇上的解釋都較為淩亂,google一下後發現國外大學的Lecture Notes 中有不少的說明,下面簡單的摘錄三段,一是c中的,二是對於java的,三是從os角度看的。

Stack vs Heap Allocation
How the memory of the computer is organized for a running program? When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment. The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.

The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack. This can be seen in Figure 14.13.
figure14.13.gif

When a program begins executing in the function main(), space is allocated on the stack for all variables declared within main(), as seen in Figure 14.13(a). If main() calls a function, func1(), additional storage is allocated for the variables in func1() at the top of the stack as ......餘下全文>>
 

c堆記憶體(Heap memory)與棧記憶體(Stack memory)有什不同?

堆是動態申請的,比如malloc或new,而棧是靜態。

而且申請的儲存空間的位置不同。
 

http://www.bkjia.com/PHPjc/868462.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/868462.htmlTechArticleC++ 效能剖析 (三):Heap Object對比 Stack (auto) Object,heapstack 通常認為,效能的改進是90 ~ 10 規則, 即10%的代碼要對90%的效能問題負責。做過...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.