Code optimization is a technique aimed at improving program efficiency by reducing resource consumption and increasing speed, while adhering to specific rules that preserve program meaning. Various optimization methods include common sub-expression elimination, copy propagation, and dead-code elimination, which help to enhance performance at different stages of code compilation. Techniques like loop optimization and peephole optimization further improve target code quality by refining the execution and redundancy of instructions.