C++ 中高维数组与一维数组的效率

发布于 2024-10-31 09:28:38 字数 231 浏览 1 评论 0原文

我很好奇使用高维数组与一维数组的效率。在定义和迭代这样的数组时,您是否会丢失任何东西:

array[i][j][k];

或者定义并迭代这样的数组:

array[k + j*jmax + i*imax];

我的倾向是不会有什么区别,但我仍在学习高效编程(我'我以前从来不需要关心这种事情)。

谢谢!

I'm curious about the efficiency of using a higher dimensional array vs a one dimensional array. Do you lose anything when defining, and iterating through an array like this:

array[i][j][k];

or defining and iterating through an array like this:

array[k + j*jmax + i*imax];

My inclination is that there wouldn't be a difference, but I'm still learning about high efficiency programming (I've never had to care about this kind of thing before).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

潜移默化 2024-11-07 09:28:38

唯一确定的方法是对两种方式进行基准测试(当然,在编译器中启用优化标志)。您认为第二种方法肯定会失去阅读的清晰度。

The only way to know for sure is to benchmark both ways (with optimization flags on in the compiler of course). The one think you lose for sure in the second method is the clarity of reading.

疯到世界奔溃 2024-11-07 09:28:38

编译后,前一种访问数组的方法和后一种方法是相同的。请记住,访问彼此靠近的内存位置确实会对性能产生影响,因为它们将以不同的方式进行缓存。因此,如果您要存储高维矩阵,并且要以这种方式访问​​行,请确保逐行存储行。

一般来说,CPU 缓存针对时间和空间顺序进行优化。也就是说,如果访问内存地址X,则访问X+1的几率会更高。对同一缓存行中的值进行操作要高效得多。

查看这篇有关 CPU 缓存的文章,了解有关不同存储策略如何影响性能的更多信息:http://en.wikipedia .org/wiki/CPU_cache

The former way and the latter way to access arrays are identical once you compile it. Keep in mind that accessing memory locations that are close to one another does make a difference in performance, as they're going to be cached differently. Thus, if you're storing a high-dimensional matrix, ensure that you store rows one after the other if you're going to be accessing them that way.

In general, CPU caches optimize for temporal and spacial ordering. That is, if you access memory address X, the odds of you accessing X+1 are higher. It's much more efficient to operate on values within the same cache line.

Check out this article on CPU caches for more information on how different storage policies affect performance: http://en.wikipedia.org/wiki/CPU_cache

倾其所爱 2024-11-07 09:28:38

如果您可以重写索引,那么编译器也可以。我不会担心这个。

相信你的编译器!

If you can rewrite the indexing, so can the compiler. I wouldn't worry about that.

Trust your compiler(tm)!

离旧人 2024-11-07 09:28:38

这可能取决于实现,但我想说它或多或少相当于一维数组的代码。

It probably depends on implementation, but I'd say it more or less amounts to your code for one-dimensional array.

你怎么敢 2024-11-07 09:28:38

帮自己一个忙,在分析代码后关心这些事情。类似的事情不太可能影响整个应用程序的性能。使用正确的算法更为重要

,即使它确实很重要,也肯定只有一个内部循环需要注意。

Do yourself a favor and care about such things after profiling the code. It is very unlikely that something like that will affect the performance of the application as a whole. Using the correct algorithms is much more important

And even if it does matter, it is most certainly only a single inner loop that needs attention.

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