堆和堆栈中数据访问性能的比较 [英] comparison of access performance of data in heap and stack

查看:165
本文介绍了堆和堆栈中数据访问性能的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,常识是,对于大多数算法而言,在堆栈上分配和取消分配数据的速度比在堆上执行分配和取消分配的速度快得多.在C ++中,代码的区别就像

It is widely known common sense, that for most algorithms, allocating and deallocating data on the stack is much faster than doing so on the heap. In C++, the difference in the code is like

double foo[n*n]

vs.

double* foo = new int[n*n]

但是,在访问和计算位于堆或堆栈上的数据时,有什么显着差异? IE.

But there are any significant differences, when it comes to accessing and calculating with data that lie either on the heap or on the stack? I.e. is there a speed difference for

foo[i]

该代码应在几种不同的体系结构上运行,因此尝试和测量将不起作用.

The code is ought to run on several different architectures, therefore try and measure will not work.

推荐答案

关于缓存局部性和读/写未命中可能存在(在很大程度上取决于系统)问题.如果您在堆栈堆数据上运行程序,则可以想象到(取决于缓存体系结构),您将遇到更多的缓存未命中的情况,而不是完全在一个连续的区域中运行它的情况.堆栈.这是SML/NJ的Andrew Appel和Zhong Shao撰写的有关此问题的论文,他们在这方面进行了确切的调查,因为堆栈/堆分配是实现功能语言的主题:

There might be (highly system depending) issues about cache locality and read/write misses. If you run your program on the stack and heap data, then it is conceivable (depending on your cache architecture) that you to run into more cache misses, than if you run it entirely on one continuos region of the stack. Here is the paper to this issue by Andrew Appel (from SML/NJ) and Zhong Shao where they investigate exactly this thing, because stack/heap allocation is a topic for the implementation of functional languages:

http://citeseerx.ist.psu.edu /viewdoc/summary?doi=10.1.1.48.3778

他们发现了一些写未命中的性能问题,但估计这些问题将通过缓存的进步得到解决.

They found some performance problems with write misses but estimated these would be resolved by advances in caching.

因此,对于现代台式机/服务器计算机,我的猜测是,除非您运行高度优化的特定于体系结构的代码,这些代码沿着缓存行流送数据,否则您不会注意到堆栈访问和堆访问之间的任何区别.对于具有较小缓存的设备(例如ARM/MIPS控制器)而言,情况可能有所不同,因为忽略缓存可能会产生明显的性能影响.

So my guess for a contemporary desktop/server machine is that, unless you're running heavily optimized, architecture specific code which streams data along the cache lines, you won't notice any difference between stack and heap accesses. Things might be different for devices with small caches (like ARM/MIPS controller), where ignoring the cache can have noticeable performance effects anyway.

这篇关于堆和堆栈中数据访问性能的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆