效率:8 个数组与多维数组
什么更有效? 8 个数组 int8 myArrayx[100]
,还是多维 int8 myArray[8][100]
?我正在使用 Microchip PIC 微控制器的 CCS 编译器,我需要在缓冲区中尽可能快地写入,这就是我提出问题的原因。
What is more efficient? 8 arrays int8 myArrayx[100]
, or a multidimensional int8 myArray[8][100]
? I'm working with CCS compiler for Microchip PIC microcontrollers, and I need to write as fast as possible in a buffer, that is the reason of my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不得不认为多维数组会更快。您有更好的机会(可能有保证?)将这些数组放入连续空间的内存中,而您无法确定 8 个单独的数组将在内存中“靠近”在一起 - 损害您的引用位置。
i would have to think that the multi-dimensional array would be faster. you have a much, much better chance (possibly guaranteed?) of having those arrays being placed into memory in contiguous space whereas you can not be sure that the 8 individual arrays will be "close" together in memory - hurting your locality of reference.
这取决于您最常访问哪些项目。
但很可能这并不重要。唯一的区别是底层内存映射分配。
It depends on which items you would access the most.
But likely it doesn't really matter. Only difference is the underlaying memory map allocation.
取决于数组的分配方式。如果在堆上,则为单个 multidim。由于引用的局部性,数组对于许多用途来说会更快,并且您可以一次性分配数组,这更简单并且产生的开销更少。
如果在堆栈或
静态
上,则生成的实际二进制代码可能完全相同。(我不确定你的设备中是否真的有堆,但我想我还是会提到它;)
Depends on how the arrays are allocated. If on the heap, then a single multidim. array would be faster for many purposes due to locality of reference and you can allocate the array in one go, which is simpler and incurs less overhead.
If on the stack or
static
, the actual binary code produced might be exactly the same.(I'm not sure you actually have a heap in your device, but I thought I'd mention it anyway ;)