When I write code I always try to make my code as clean and readable as possible.

Every now and then there comes a time when you need to cross the line and go from nice clean code to slightly uglier code to make it faster.

When is it OK to cross that line?

You cross the line when


You have measured that your code is too slow for its intended use.
You have tried alternative improvements that don't require mucking up the code.


Here's a real-world example: an experimental system I am running was producing data too slowly, taking over 9 hours per run and using only 40% of CPU.  Rather than mess up the code too much, I moved all the temporary files to an in-memory filesystem.  Added 8 new lines of non-ugly code, and now CPU utilization is above 98%.  Problem solved; no ugliness required.
