Syllabus Lesson 18 of 246 · Module 1: Python for this course
Module 1: Python for this course

Finding the slow part

Why it matters When training is slow, the cause is nearly always one line. Measure before you change anything.

A profiler runs your program and reports how long each line took. The rule that almost always holds: one or two lines take most of the time, and speeding up anything else is wasted effort.

Tiny numbers

If a run takes 10 seconds and one line takes 8 of them, making every other line infinitely fast saves only 2 seconds. Making that one line twice as fast saves 4.

Profiler: a tool that measures where a program spends its time.

Recap Find the biggest number first. Fix that, then measure again.

Your turn

profile maps each line of a training script to the seconds it took. Write slowest(profile) that returns a pair: the slowest line, and its share of the total time as a fraction (0 to 1).

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.