内部循环与外部循环分配内存 [英] Allocating memory inside loop vs outside loop

查看:155
本文介绍了内部循环与外部循环分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每次循环迭代中分配大块的堆内存是否存在明显的性能损失?当然,我会在每次迭代结束时释放它.

Is there noticeable performance penalty for allocating LARGE chunks of heap memory in every iteration of loop? Of course I free it at the end of each iteration.

另一种选择是在进入循环之前分配一次,在所有迭代中重复使用它,并最终在退出循环后释放它.请参见下面的代码.

An alternative would be to allocate once before entering the loop, repeatedly using it in all iterations, and eventually freeing it up after exiting the loop. See the code below.

// allocation inside loop
for(int i = 0; i < iter_count; i++) {
    float *array = new float[size]();
    do_something(array);
    delete []array;
}

// allocation outside loop
float *array = new float[size]();
for(int i = 0; i < iter_count; i++) {
    do_something(array);
}
delete []array;

推荐答案

  • 即使分配是固定的时间,您也将拥有TxN而不是T.此外,如果您对块进行了任何内存初始化(即使只是将其设置为零),您也将反复刷新缓存.
  • 堆分配的主要性能问题是碎片化,而不是分配时间,这是一个累积性问题.少积累.

    • Even if allocation was constant time, you have TxN instead of T. In addiiton, if you have any memory initialization of the chunk (even if it's just setting to zero), you repeatedly thrash your cache.
    • The major performance hit of heap allocations is fragmentation, not allocation time, which is an accumulative problem. Accumulate less.

      有些病理情况.如果有很多短暂的分配活动跨越"了块的重新分配和分配(例如,在另一个线程中运行相同的例程),那么您可能会经常推动堆管理器为大块请求新的内存(因为它是当前的)占据).这确实会分散您的缓存并增加您的工作集.

      There are some pathological cases. If there's a lot of short-lived allocation activity that "spans" deallocation and allocation of the chunk (e.g. running the same routine in another thread), you might frequently push the heap manager to require new memory for the big chunk (because it's currently occupied). That will really fragment your cache and increase your working set.

      直接命中可以直接衡量:new/deletedo_something()相比要花费多少?如果do_something昂贵,您可能无法衡量.

      So there's the direct hit, which can be measured directly: how much does new/delete cost compared to do_something()? if do_something is expensive, you might not measure much.

      在大型应用程序中会累积堆压".对此的贡献很难衡量,您可能会遇到由十几个独立贡献者构建的绩效砖墙,事后很难确定.

      And there's the "heap pressure" which accumulates in a large application. The contribution to that is hard to measure, and you might hit a performance brick wall built by a dozen independent contributors, which are hard to identify after the fact.

      这篇关于内部循环与外部循环分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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