liveBook · Manning

Worst-case performance

As explained in Practices of the Python Pro

Worst-case complexity is the maximum resource requirement an algorithm can incur over inputs of a given size.

Worst-case performance describes the maximum time or resources an algorithm may require for inputs of a given size. Big O notation expresses this upper-bound behavior. section 4.1.1

Checking for duplicates

A duplicate checker can stop quickly when the first two items match. In the worst case—no duplicates, or duplicates appearing last—it compares all items through nested loops, producing O(n²) time. section 4.1.2

Use worst-case analysis to:

  • Compare alternative algorithms for the same task. section 4.1.1
  • See how increasing input size affects execution time. section 4.1.2
def has_duplicates(sequence):
    for index1, item1 in enumerate(sequence):        #1
        for index2, item2 in enumerate(sequence):    #2
            if item1 == item2 and index1 != index2:  #3
                return True
    return False

Inspect the nested-loop duplicate checker and identify the input arrangement that forces its worst case. section 4.1.2

How Practices of the Python Pro covers it

How deep it goes
Defines Big O and demonstrates O(n²) with nested loops, while also mentioning best-case and average-case analysis.
Examples
Uses a Python duplicate checker whose runtime varies according to where matching items appear.
What you can do after
Evaluate same-task algorithms by estimating their worst-case growth and spotting nested loops that produce O(n²).
How it connects
Connects asymptotic analysis with Big O notation, time complexity, best-case analysis, average-case analysis, and constant time.

Also explained in

Compare how 3 books explain worst-case performance →

Related concepts

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