AI Hints
Home›DSA in Python Tutorial›Getting Started
DSA in Python · Lesson 3 of 132

Why It Matters

Speed at scale.

⏱ 2 min read▶ Video at 2:15🎓 Free
Watch this lesson in the DSA in Python courseWatch on YouTube ↗Full DSA in Python course

With ten items, almost any code is fast. But real programs handle millions of users, products, and messages. That's when the choice of data structure and algorithm turns a program that takes a second into one that takes a day.

Why It Matters
From the video: Why It Matters
  • Ten items: everything is fast
  • Millions: choices matter
  • A second, or a day

A list or a set?

Let's search for a number among one million. First, in a list, which checks items one by one. Then in a set, which jumps straight to the answer. Time it measures how long each search takes, a thousand times over. Run it. The set is many thousands of times faster, for exactly the same question.

speed.pypython
import timeit
nums_list = list(range(1_000_000))
nums_set = set(nums_list)

t1 = timeit.timeit(lambda: 999_999 in nums_list, number=1000)
t2 = timeit.timeit(lambda: 999_999 in nums_set, number=1000)
print(f"list: {t1:.3f} seconds")
print(f"set:  {t2:.5f} seconds")
print(f"the set is about {t1 / t2:,.0f} times faster")
Output
list: 4.571 seconds
set:  0.00003 seconds
the set is about 172,489 times faster

Where it shows up

These ideas are everywhere. Search engines, maps finding the fastest route, social networks suggesting friends, and the autocomplete on your phone. They're also the most common topic in programming job interviews. That's why every developer learns them.

Searchfinding things fast
Mapsshortest routes
Social appsfriends of friends
Interviewsthe classic topic

Summary

The right choice can make code thousands of times faster. That's why data structures and algorithms matter. Next, how to measure speed properly.

Key takeaways
  • Small data hides problems
  • Big data reveals them
  • A set beat a list by thousands
Next up: Counting Steps — measuring speed fairly