Experience is the best teacher. Keep writing code, revisit old code and rewrite it.
Also, is worth knowing when not to optimise. Code you can read is code you can maintain, and some optimisations are not as readable.
Learn how to use a profiler. Its a bit of an artform, but learning to interpret the results will help you find slow code sections. It'll also help you determine if your optimisations are actually worthwhile. Measure first, optimise second.