-O3 Flag Disappoints Me

Hello everyone!

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?

Thank you for the help.
Last edited on
If you want to speed things up, you really should try to move expensive operations out of loops. There's often a trade-off between clarity and speed.

I remember Stroustrup saying, "Keep and eye on efficiency."
Last edited on
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?
closed account (o1vk4iN6)
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".
Last edited on
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?


@webJose
Is there a library that does that?
I don't know of any libraries that define custom heap classes.
If you have determined that memory allocation is slowing your down
That. First determine the bottleneck, then solve it.
If you want, post the code.

¿Is http://cplusplus.com/forum/beginner/74024/ related? I would suggest to use an array instead. (stack)

By the way, I've got better results with -O2 one time http://www.cplusplus.com/forum/general/63574/2/#msg345062
Last edited on
Topic archived. No new replies allowed.