Cost of memory [de]allocation and potential compiler optimizations (c++) -
is cost of memory [de]allocation defined? if cost depends upon specific compiler being used, there general way memory [de]allocation implemented such can reasonably assume cost?
is compiler able optimize following code such call 'new' done once?
char * arr = null; (size_t = 0; < 5000000000; ++i) { arr = new char[100000000] ... // process things here delete []arr; }
the compiler not able perform optimization. @ lowest level, storage allocation boils down calls library functions such malloc (and, 1 layer deeper, os apis). compiler it's not safe assume individual malloc/free pairs can left out , storage reused because implementation should outside optimizer's scope.
apart that, don't think job optimizer. that's you, programmer, can without special efforts.
there no standardized cost memory allocation/deallocation. generally, allocation/deallocation times may vary (for example take longer if user-space heap implementation forced fetch fresh pages os kernel's memory manager).
a reasonable rule of thumb small allocations faster larger ones , allocations should slower de-allocations.
Comments
Post a Comment