堆VS堆栈关于读取/写入速度 [英] Heap vs Stack in regard to read/write speed

查看:305
本文介绍了堆VS堆栈关于读取/写入速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚经历了一堆堆叠VS堆在这里的线程堆栈溢出(并通过谷歌找到了其他随机网站)的去,但我找不到它提供太多的答案(如果有的话)深度阅读的问题/写速度(最答案,文章着重分配速度)。

I just went through a bunch of stack vs heap threads here on Stack Overflow (and other random sites found through Google), and yet I can't find an answer that provides much (if any) depth to the issue of read/write speed (most answers and articles focus on allocation speed).

如果我正在写一个C程序(在我的情况下,游戏)在那里我存储在缓冲区的大小几KB对象数据,它会有所作为(在访问速度方面)如果缓冲器被分配在堆栈或堆?或将编译器把该大小的缓冲区相同的两种方式,当谈到决定在哪里硬件(即高速缓存或内存)来存储呢?

If I'm writing a C program (in my case, a game) where I'm storing object data in a buffer that's a few kilobytes in size, will it make a difference (in terms of access speed) if that buffer is allocated on the stack or on the heap? Or will the compiler treat a buffer of that size the same either way when it comes to deciding where on the hardware (i.e. cache or RAM) to store it?

推荐答案

在一个典型的现代CPU和OS,不,我不希望高速缓存未命中的平均访问时间任何区别,可能性,或(更糟糕的)可能性页错误的基础上,缓冲器是否处于堆或堆。应该没关系的唯一事情是你访问它的格局,相比其他使用内存。然而,也有可能被嵌入或其中堆叠保持在不同种类的存储器比堆的专用平台。

On a typical modern CPU and OS, no, I wouldn't expect any difference in average access time, likelihood of cache miss, or (much worse) likelihood of page fault based on whether the buffer is in heap or stack. The only thing that should matter is your pattern of access to it, compared to other use of memory. However, there may be embedded or special-purpose platforms where the stack is kept in a different kind of memory than the heap.

你忽略第三种可能性,非堆内存(通过一些特定的操作系统调用分配,如 MMAP ,而不是的malloc )。

You're ignoring a third possibility, non-heap memory (allocated by some OS-specific call, such as mmap, rather than malloc).

最后,如果你想彻底删除页面错误的可能性,操作系统可以提供一种方式来做到这一点;例如, MLOCK 的POSIX。

Finally, if you want to completely remove the possibility of page faults, the OS may provide a way to do that; for example, mlock on POSIX.

这篇关于堆VS堆栈关于读取/写入速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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