在有限递归循环中操作大型数组时,我应该从堆栈切换到堆的什么点?
我对 C++ 非常陌生,正在尝试优化在递归循环中对大型一维数组中的元素执行计算的程序的速度。事实上,一维数组存储 3D 矩阵的值,每个值都是浮点。矩阵的尺寸可能高达 1500 x 2000 x 200。
我读到堆栈内存的读取速度可能比堆内存更快,但是是否存在大小限制,如果我设置堆栈大小,它会变得低效甚至不切实际足够大以容纳所有数据吗?
感谢您的帮助,如果这是一个愚蠢的问题,我们深表歉意!
PS 如果您能建议我应该研究的任何其他技术来加速我的程序,我将非常感激!
I am very new to C++ and am trying to optimise the speed of a program which performs calculations on elements in a large one-dimensional array in a recursive loop. In fact the one-dimensional array is storing the values of a 3D matrix, each value a floating point. The dimensions of the matrices may be up to 1500 x 2000 x 200.
I have read that stack memory may be read faster than heap memory, but is there a size limit at which it becomes inefficient or even unpracticable, assuming I set the stack size large enough to hold all the data?
Thanks for your help, and apologies if this is a stupid question!
PS If you can suggest any other techniques I should look into to speed up my program, I would be much obliged!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
堆栈内存和堆内存之间的速度没有区别。优化代码的最佳方法是以线性方式遍历数组以最大化缓存利用率。
There's no difference in speed between stack and heap memory. The best way to optimize the code is to traverse the arrays in a linear fashion to maximize cache utilization.
不。不过,您可能想将递归循环更改为迭代循环。除非它是尾递归循环,并且编译器足够聪明来优化它,例如 g++ 从 -O2 开始。
No. You might want to change the recursive loop into an iterative loop, though. Unless its a tail recursive loop and the compiler is smart enough to optimize it, which g++ does starting at -O2, for example.
事实上,在定义它们的函数内,堆栈变量可能比堆变量读取得更快。所有其他函数仅接收对变量的指针/引用,这与堆的指针/引用类似- 分配的变量。
将大变量放在堆栈上通常是不明智的,因此我建议采取保守的方法并将数组放在堆上(例如,使用
std::vector
)。最后,这个微小的差异不太可能影响程序的整体速度。
优化通常是专家的领域,或者至少是那些深入研究分析器并对底层语言语义和硬件以及如何从高级到低级进行翻译有足够了解的人......专家,或想成为专家的人那么:)
如果你的函数仍然太慢,那么..发布一些代码!尽管它可能更适合 codereview.stackexchange.com。
Stack variables might, indeed, be read faster than heap variables within the function they are defined in. All other functions only receive a pointer/reference to the variable which is similar to what they would have for a heap-allocated variable.
It is generally unwise to put large variables on the stack, so I will suggest to take the conservative approach and put your array on the heap (use a
std::vector
, for example).Finally, this small difference is very unlikely to affect the overall speed of your program.
Optimization is generally the domain of experts, or at least people who dig enough into profilers and have sufficient understanding of the underlying language semantics and hardware and how the translation is done from high-level to low-level... Experts, or wannabe experts then :)
If your function is still too slow, then... post some code! Though it may be better suited for codereview.stackexchange.com.