最小的malloc量()调用提高性能? [英] Minimizing the amount of malloc() calls improves performance?

查看:85
本文介绍了最小的malloc量()调用提高性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑两个应用:(NUM 1)一个调用的malloc()多次,另调用的malloc()几次(NUM 2)。
这两个应用程序分配的内存相同的量(假设100MB)。结果
对于应用在未来的malloc()调用会更快,#1或#2?结果
换句话说:是否的malloc()具有分配位置的索引在内存

Consider two applications: one (num. 1) that invokes malloc() many times, and the other (num. 2) that invokes malloc() few times. Both applications allocate the same amount of memory (assume 100MB).
For which application the next malloc() call will be faster, #1 or #2?
In other words: Does malloc() have an index of allocated locations in memory?

推荐答案

当然,这完全取决于malloc的实现,但在这种情况下,没有调用自由,最malloc的实现可能会给予你同样的算法速度。

Of course this completely depends on the malloc implementation, but in this case, with no calls to free, most malloc implementations will probably give you the same algorithmic speed.

作为另一个答案评论说,通常会有免费块的列表,但如果你没有自由调用,有也只是一个,所以它应该(1)在这两种情况下为O

As another answer commented, usually there will be a list of free blocks, but if you have not called free, there will just be one, so it should be O(1) in both cases.

这假定为堆分配内存在两种情况下足够大。如果#1,你会分配更多的内存总量,因为每个分配涉及的内存开销来存储元数据,因此你可能需要调用SBRK(),或相当于种植堆的情况下,#1,这将添加一个额外的开销。

This assumes that the memory allocated for the heap is big enough in both cases. In case #1, you will have allocated more total memory, as each allocation involves memory overhead to store meta-data, as a result you may need to call sbrk(), or equivalent to grow the heap in case #1, which would add an additional overhead.

它们将可能是由于高速缓存和其它二阶效应不同,由于用于新分配的存储器比对不会是相同的。

They will probably be different due to cache and other second order effects, since the memory alignments for the new allocation won't be the same.

如果您已释放了一些存储器块,则它很可能是#2将更快由于较少的碎片,因此空闲块的较小列表来搜索。

If you have been freeing some of the memory blocks, then it is likely that #2 will be faster due to less fragmentation, and so a smaller list of free blocks to search.

如果您已释放所有的内存块,应该最终会被完全一样的,因为任何理智的免费实施将对合并块放回内存的一个舞台。

If you have freed all the memory blocks, it should end up being exactly the same, since any sane free implementation will have coalesced the blocks back into a single arena of memory.

这篇关于最小的malloc量()调用提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆