Is accessing statically or dynamically allocated memory faster?

后端 未结 5 817
无人及你
无人及你 2021-01-07 23:27

There are 2 ways of allocating global array in C:

  1. statically

    char data[65536];
    
  2. dynamically

    char *d         
    
    
            
5条回答
  •  隐瞒了意图╮
    2021-01-07 23:51

    Calculating offsets is not a big performance issue. You have to consider how you will actually use the array in your code. You'll most likely write something like data[i] = x; and then no matter where data is stored, the program has to load a base address and calculate an offset.

    The scenario where the compiler can hard code the address in case of the statically allocated array only happens when you write something like data[55] = x; which is probably a far less likely use case.

    At any rate we are talking about a few CPU ticks here and there. It's not something you should go chasing by attempting manual optimization.

    Each memory access is equivalent to about 40 CPU clock cycles

    What!? What CPU is that? Some pre-ancient computer from 1960?

    Regarding cache memory, those concerns may be more valid. It is possible that statically allocated memory utilizes data cache better, but that's just speculation and you'd have to have a very specific CPU in mind to have that discussion.


    There is however a significant performance difference between static and dynamic allocation, and that is the allocation itself. For each call to malloc there is a call to the OS API, which in turn runs search function going through the heap and looking for for a free segment. The library also needs to keep track of the address to that segment internally, so that when you call free() it knows how much memory to release. Also, the more you call malloc/free, the more segmented the heap will become.

提交回复
热议问题