The cost of memory allocation in a loop in C

江枫思渺然 提交于 2019-12-24 10:55:38

问题


Is there a significant difference between allocating an array inside or outside a loop in terms of cost of time?

I use many arrays in functions inside a loop in my program, should I pass all the arrays as function parameters to increase the performance although it decreases the readability? For example:

#include <stdlib.h>
#define N 1000000
void foo()
{
    int* array = (int*)malloc(N*sizeof(int));
    /*
    Do something with the array
    */
    free(array);
}
int main()
{
    int i;
    for(i=0; i<1000000; i++)
        foo();
    return 0;
}

or

#include <stdlib.h>
#define N 1000000
void foo(int* array)
{
    /*
    Do something with the array
    */
}
int main()
{
    int i;
    int* array = (int*)malloc(N*sizeof(int));
    for(i=0; i<1000000; i++)
        foo(array);
    free(array);
    return 0;
}

回答1:


The cost of a memory allocation is not very dependent on the size of the allocation. Roughly speaking, a memory allocation of any size is O(1), and obviously standard libraries are optimized to make allocations as fast as possible.

So if you need a very big allocation, as in the example program, the cost of allocation will be trivial compared with the cost of initializing the allocated memory (to say nothing of the cost of actually doing the computation which is required).

For small allocations in very tight loops, where the allocation overhead might be noticeable, alternative mechanisms might be useful; one of these is the one suggested in the question, passing a preallocated array as an additional parameter to the function. (Other possibilities include using C's variable length arrays (VLAs), if they are available on all target platforms, or alloca/_alloca/_malloca.)

But I would suggest not implementing microoptimizations of this form until there is solid evidence that the time savings are justified; otherwise, the cost in maintainability and readability will outweigh any small time savings you might achieve.




回答2:


The second example is better with regards to time. Assuming the thing you would do with the array in 'foo' for each example is the same.

In the second example: You allocate one array, and do something to it a bunch of times. Then after you do that destroy it.

In the first example: each iteration of the loop you create a new array, do something and then destroy it. This means that extra time is taken to create and destroy the array each iteration of the loop.




回答3:


Declaring the array and then using it strikes me as the better way to do things.

Your declaration takes place in a higher-level body of code and happens once: therefore it is easy to understand what is going on, change the allocation size, handle allocation errors, and debug.

Declaring the array in the for loop raises a definite code smell for me: it is a solution that lacks modularity.



来源:https://stackoverflow.com/questions/28919465/the-cost-of-memory-allocation-in-a-loop-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!