Lecture notes · Programming in Python · Lecture 21

Timing Programs and Counting Operations

MIT 6.100L Intro to CS and Programming using Python, lecture 21, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part explains why a program must be fast as well as correct, and introduces simple ways to measure how efficient a program is.

Key ideas

Words to know

memoization
Saving results you have already worked out in a memo, such as a dictionary, so you do not have to calculate them again.
epoch
The starting point of time that computers count from, which is January 1, 1970. The time module counts seconds from this point.
implementation
One specific way of writing a program in code. Two different implementations can carry out the same underlying algorithm.
module
A file of ready-made functions and tools that you bring into your code, such as the time module, so you can use them.

Example

Imagine a student who writes each answer in a notebook the first time they work it out. The next time the same question comes up, they just read the answer instead of working it out again. That saves time but uses some notebook space, which is the same tradeoff memoization makes.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part shows how to time Python programs, why timing alone is unreliable, and how counting basic steps gives a fairer way to compare algorithms.

Key ideas

Words to know

wrapper function
A function that wraps around another function to add extra behavior, such as timing it, without changing the inner function.
algorithm
A set of steps for solving a problem. The lesson says there are only a handful of these for any given problem.
implementation
One specific way of writing an algorithm in code. The same algorithm can be written as many different implementations.
operation
One basic action in a program, such as adding, comparing, or assigning a value.

Example

Think of timing a recipe with a kitchen clock. The time depends on your oven, your speed, and whether the phone rings during cooking. If you instead count the steps in the recipe, such as stirring, measuring, and pouring, you get a number that stays the same no matter whose kitchen you use.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–32:04

This part shows how to count the basic steps a program takes, so you can predict how its work grows as the input gets bigger.

Key ideas

Words to know

operation
One simple step the computer does, like assigning a value to a variable or adding two numbers.
nested for loop
A for loop placed inside another for loop, so the inner loop runs again every time the outer loop runs once.
steady state
The pattern a set of results settles into when the input gets very large.
counter
A variable that goes up by one each time a step runs, so the program can keep track of how many steps it has done.

Example

Imagine a teacher who checks each paper three times, and then does a few quick tasks at the end. With 10 papers, those few extra tasks are a big part of the work. With 10,000 papers, they hardly change the total, so the teacher's work mostly grows with 3 checks per paper.

3 checked practice questions for this part are in the app.

← Lecture 19: Inheritance · Lecture 22: Big Oh and Theta →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York