在有限递归循环中操作大型数组时,我应该从堆栈切换到堆的什么点?

发布于 2024-11-30 19:48:01 字数 264 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄凉少年不暖心 2024-12-07 19:48:01

堆栈内存和堆内存之间的速度没有区别。优化代码的最佳方法是以线性方式遍历数组以最大化缓存利用率。

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.

那一片橙海, 2024-12-07 19:48:01

假设我将堆栈大小设置得足够大以容纳所有数据,是否存在大小限制,达到该限制,它会变得低效甚至不切实际?

不。不过,您可能想将递归循环更改为迭代循环。除非它是尾递归循环,并且编译器足够聪明来优化它,例如 g++ 从 -O2 开始。

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?

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.

假扮的天使 2024-12-07 19:48:01

事实上,在定义它们的函数内,堆栈变量可能比堆变量读取得更快。所有其他函数仅接收对变量的指针/引用,这与堆的指针/引用类似- 分配的变量。

将大变量放在堆栈上通常是不明智的,因此我建议采取保守的方法并将数组放在堆上(例如,使用 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 :)

  • prefer optimizing the algorithm rather than its implementation (to begin with)
  • don't jump (randomly) around memory

If your function is still too slow, then... post some code! Though it may be better suited for codereview.stackexchange.com.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文