登录  /  注册

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

php中文网
发布: 2016-07-13 10:20:09
原创
1140人浏览过

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

                    {

                                        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

                    {

                                        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,而栈是静态的。

而且申请的存储空间的位置不同。
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/868462.htmlTechArticleC++ 性能剖析 (三):Heap Object对比 Stack (auto) Object,heapstack 通常认为,性能的改进是90 ~ 10 规则, 即10%的代码要对90%的性能问题负责。做过...
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号