I've been working on a program whose body contains a quadruple-nested loop. In each of the three inner-most loops, there is allocation of around 100B of memory on average. The program runs really slow even with -O3 flag raised. I am really concerned about allocating memory on every iteration of each loop, but I have kept it this way because someone in this forum (with around 6000 posts or so) recommended that it is better to keep the declaration as close to the initialization of variables as possible because it makes the compiler's life easier when trying to optimize code.
Would you suggest anything to make the program run faster?
If, for example, memory-allocating functions are expensive, probably moving them out of the loops in which they are used would be reasonable, yes. But, what is the point of doing that with full optimization? Shouldn't the compiler be concerned with that?
If you wrote code for a sort algorithm like bubble sort can you really expect the compiler to make it as fast as quick sort (less comparisons) ? That's your job as a programmer to determine which algorithm to use. Same thing goes for when you are designing your own. You should have at least basic knowledge of what's "slow" and "fast".
If you have determined that memory allocation is slowing your down, then welcome to one of the main issues in game programming!
Game programmers usually allocate big chunks of memory and then create a heap class that administrates it. It is not a trivial chore, but alas, it has to be done.
@xerzi
So, with optimzation enabled, should I allocate memory inside or outside the loop? Or it doesn't matter because the compiler seems to do that anyway?