This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: optimizing constant ifs out of loop?


Hi David,

Sometimes, you have to take the bull by the horns and lead it to the water.

IF you have profiled your code, and know that this tight loop is a bottleneck, then you can-and-should reorganize the code so that it is more palatable to the compiler to produce more efficient code.

If that means that you have to have a whole slew of nested for-loops (via macro?), then so be it.  Comment your code heavily so as to explain why you've implemented something in a more obtuse fashion.

If that means you have to implement the routine in lovingly hand crafted assembly... then there you go.

IF you have NOT profiled the code, then optimizing for performance (in lieu of the norm:  readability) is like taking potshots in the dark.

Other ways that you could improve readability while concurrently (perhaps) improve performance is to move each of the loops into a separate function.  That way, the branching logic is segregated from the looping algorithm.  Which reduces the cyclical complexity -- something that degrades readability / maintainability.

"Premature optimization is the root of all evil."
~ Tony Hoare (often misattributed to Donald Knuth, who was quoting Tony Hoare)

Sincerely,
--Eljay



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]