简体   繁体   English

堆上的分配是否会影响访问性能?

[英]Does allocation on the heap affect the access performance?

As known: 众所周知:

ptr = malloc(size);

or in C++ 或在C ++中

ptr = new Klass();

will allocate size bytes on the heap. 将在堆上分配大小字节。 It is less efficient than on the stack. 它比堆栈上的效率低。

But after the allocation, when we access it: 但是在分配之后,当我们访问它时:

foo(*ptr);

or 要么

(*ptr)++;

Does it have the same performance as data on the stack, or still slower? 它与堆栈上的数据具有相同的性能还是更慢?

The only way to definitively answer this question is to code up both versions and measure their performance under multiple scenarios (different allocation sizes, different optimiziation settings, etc). 明确回答这个问题的唯一方法是对两个版本进行编码,并在多种情况下(不同的分配大小,不同的优化设置等)衡量它们的性能。 This sort of thing depends heavily on a lot of different factors, such as optimization settings, how the operating system manages memory, the size of the block being allocated, locality of accesses, etc. Never blindly assume that one method is more "efficient" than another in all circumstances. 这种事情在很大程度上取决于许多不同的因素,例如优化设置,操作系统如何管理内存,分配的块的大小,访问的位置等。 永远不要盲目地认为一种方法更“有效”。在所有情况下都比另一个要好。

Even then, the results will only be applicable for your particular system. 即使这样,结果也仅适用于您的特定系统。

It really depends on what you are comparing and how. 这实际上取决于您正在比较什么以及如何进行比较。

If you mean is 如果你的意思是

ptr = malloc(10 * sizeof(int)); 

slower than: 慢于:

int arr[10]
ptr = arr; 

and then using ptr to access the integers it points at? 然后使用ptr访问它指向的整数?

Then no. 那就不要。

If you are referring to using arr[0] instead of *ptr in the second case, possibly, as the compiler has to read the value in ptr to find the address of the actual variable. 如果要在第二种情况下使用arr[0]而不是*ptr ,则可能是因为编译器必须读取ptr的值以查找实际变量的地址。 In many cases, however, it will "know" the value inside ptr , so won't need to read the pointer in itself. 但是,在许多情况下,它将“知道” ptr的值,因此不需要自己读取指针。

If we are comparing foo(ptr) and foo(arr) it won't make any difference at all. 如果我们要比较foo(ptr)foo(arr)那根本没有任何区别。

[There may be some penalty in actually allocating on the heap in that the memory has to be "committed" on the first use. [在堆上实际分配可能会有一些损失,因为在第一次使用时必须“承诺”内存。 But that is at most once for each 4KB, and we can probably ignore that in most cases]. 但这每4KB最多只能有一次,在大多数情况下,我们可能会忽略它。

Efficiency considerations are important when comparing an algorithm that runs in O(n^2) time versus O(nlogn), etc. 在比较以O(n ^ 2)时间与O(nlogn)等时间运行的算法时,效率方面的考虑很重要。

Comparing memory storage accesses, both algorithms are O(n) or O(k) and usually is is NOT possible to measure any difference. 比较存储器存储访问,两种算法为O(n)或O(k)和通常是不可能测量的任何差异。

However, if you are writing some code for the kernel that is frequently invoked than a small difference might become measurable. 但是,如果您正在为频繁调用的内核编写一些代码,则可能会有很小的差异。

In the context of this question, the real answer is that it really doesn't matter, use whichever storage makes your program easy to read and maintain. 在这个问题的上下文中,真正的答案是,这并不重要,无论使用哪种存储方式,都使您的程序易于阅读和维护。 Because, in the long run the cost of paying humans to read your code is more than the cost of a running a cpu for a few more/or less instructions. 因为,从长远来看,付出人工来阅读代码的代价要比运行cpu来获取更多(或更少)指令的成本高。

Stack is much faster than Heap since it involves as simple as moving the stack pointer. 堆栈比堆快得多,因为它涉及到移动堆栈指针一样简单。 stack is of fixed size. 堆栈大小固定。 in contrast to Heap, user need to manually allocate and de-allocate the memory. 与堆相比,用户需要手动分配和取消分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM