liveBook · Manning

Premature optimization

As explained in Learn AI-Assisted Python Programming, Second Edition

Premature optimization is the practice of optimizing a program before establishing that its current implementation is inadequate or identifying the relevant performance bottleneck.

Premature optimization means improving a program before testing whether it is fast enough or identifying a real performance problem. A solution that seems slow may already meet its needs, while a more sophisticated version can be harder to get right. section 9.5.1

Use this practical decision sequence:

  • Try the simple implementation first. section 9.5.1
  • Keep it if it is fast enough and completes the job. section 9.5.1
  • Consider a faster solution only when the program is genuinely too slow or used often enough to justify extra effort. section 9.5.1

Comparing duplicate files

A Python script compares every pair of PNG files in a directory and may become slow for large directories. Rather than immediately replacing it, run it first and optimize only if its performance causes a real problem. section 9.5.1

```python
import os
import filecmp
 
# Replace this with the actual directory path where your .png 
# files are located
directory = '/path/to/directory'
 
# Iterate over all the files in the directory
for filename1 in os.listdir(directory):
    if filename1.endswith('.png'):
        for filename2 in os.listdir(directory):
            if filename2.endswith('.png') and filename1 != filename2:
                file1 = os.path.join(directory, filename1)
                file2 = os.path.join(directory, filename2)
                if filecmp.cmp(file1, file2):
                    print(f'{filename1} and {filename2} are duplicates.')
```

Inspect the straightforward nested-loop implementation that illustrates a deliberately unoptimized starting point. section 9.5.1

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains the timing and risks of optimization but stops short of teaching faster implementation techniques.
Examples
Uses a Python directory script that compares PNG files pairwise and may slow down on large directories.
What you can do after
Evaluate whether a simple implementation is sufficient before investing in a more sophisticated solution.
How it connects
Ties optimization to program correctness and performance requirements.

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 Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.