liveBook · Manning

Premature optimization

As explained in Modern C

Premature optimization is modifying code for presumed performance gains before establishing that the gains are relevant and necessary.

Premature optimization modifies code for presumed performance gains before establishing that the gains are relevant and necessary. In C, clever performance changes can harm readability, soundness, maintainability, and safety. TAKEAWAY: Do not trade off safety for performance. section 15.0

Prefer simple safeguards before attempting clever optimization:

  • Initialize every block-scope variable. section 15.0
  • Use calloc instead of malloc where suitable to reduce uninitialized-object problems. section 15.0
  • Provide initialization functions for more complicated dynamically allocated data structures. section 15.0

How Modern C covers it

How deep it goes
Defines the hazard and lists consequences and safeguards, but stops short of providing a measurement or optimization procedure.
Examples
Uses uninitialized objects, invalid array access, ended object lifetimes, integer overflow, and clever code as concrete risks.
What it assumes
Assumes comfort with C programming while reinforcing initialization, allocation, and pointer-related safety practices.
What you can do after
Evaluate performance changes against safety, readability, soundness, and maintainability before adopting them.
How it connects
Connects performance with readability, soundness, maintainability, safety, initialization, and dynamic memory allocation.

Also explained in

Compare how 2 books explain premature optimization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.