The first rule of optimization is—don't optimize. You should always remember this phrase by Donald Knut:
Premature optimization is the root of all evil
This is a very true and correct statement. You should start doing performance optimization only when you see a performance problem and you have found what is causing it.
There are two types of performance optimization:
Explicit
Implicit
Explicit performance optimization is a technique that is directed at a specific slow piece of code. This type of optimization requires significant code changes that could worsen code readability. You do explicit performance optimization by changing the algorithm to a more effective one. Using more memory for the cache could also increase performance.