Martin Fowler recently added a discussion on whether optimization is refactoring to his bliki. His conclusion is that optimizations are not refactorings, but that a particular change made to a program might be both.
I believe that unless you are working with inefficient programming environments such as browser scripting or you have extreme performance requirements you should avoid performance optimizations to a great extent. The reason is that it is very, very hard to determine whether what you believe is an optimization actually will improve performance. Michael Teper has a spot on article showing that what might look like an optimization might not optimize the performance. The article also shows that the only way to optimize code for better performance is to measure execution times, since relying on assumptions might slow down the execution.
The original code, which also had the best performance, was much clearer than the optimizations. A key motivation for refactoring code is to make it easier to understand. Other motivations are unifying duplicate code and making it cheaper to modify. As Teper’s examples show, an optimization can make the code more difficult to read and therefore it cannot be viewed as refactored by these criterions.
Kent Beck, who was a contributor to Fowlers Refactoring book, also includes “Make the program run faster” and “Replace one algorithm with another” as motivations for refactoring. The latter of these is not really a motivation, but more over a method for improving performance. For example sorting 1000 random numbers in an insertion sort algorithm takes about 7.5 seconds, while two-way quick sort takes approximately 100 milliseconds. Even though refactoring is never mentioned, Jon Bentley’s seminal book Programming Pearls shows many similar optimizing refactorings.
I regard exchanging an inefficient algorithm with a better one as a refactoring pattern and the reason for applying it is to increase performance. Therefore I do not fully agree with Fowler that optimization and refactoring have different purposes. At times they have, at others they have not.
As Tepers examples show optimizations are not refactorings, but refactorings can be optimizations, and often optimization is achieved as a byproduct of refactoring. If you improve the design of code to increase performance you most likely apply a refactoring pattern and hence an optimization can be a refactoring.