# Refactoring & Performance

* Refactoring can certainly make software go more slowly - but it also makes software more amenable to performance tuning.
* The secret to fast software is writing tunable software first and then tune it for sufficient speed.

## Three General Approaches to Writing Fast Software

### 1. Time Budgeting

* Often used in hard real-time systems.&#x20;
* Each component is given a budget for resources (time & footprint)
* Essential for systems in which late data is bad data (e.g. heart pacemaker)
* <mark style="color:orange;">This technique is NOT appropriate for corporate information systems.</mark>

### 2. Constant Attention

* Every programmer, all the time, does whatever they can do to keep performance high.
* Common approach.&#x20;
* <mark style="color:red;">Intuitively attractive but doesn't work well</mark>.
* Changes to improve performance usually make the program harder to work with, slowing development.
* Performance improvements are spread all around the program, often with a misunderstanding of how a compiler, runtime, & hardware behaves.
* 90% of this work is wasted because it's optimizing code that isn't run much.

### 3. Focused Attention

* Write well-factored code without paying attention to performance.
* Begin a deliberate performance optimization exercise.
  * Run the program under a profiler that monitors where it's consuming time & space.
  * Look for hot spots.
  * Tune performance in the hot spot areas using the same techniques one would use in the "Constant Attention" approach.
* Well-factored code provides granularity for performance analysis.&#x20;
* Smaller parts of code are easier to tune.
