1 of 12

CSE 163

Introduction to Efficiency�

Suh Young Choi�

🎶 Listening to: David Garrett

💬 Before Class: Most anticipated media release of the year? (Has it already happened?)

2 of 12

Announcements

Y’all did it! HW4 is due tonight!

THA5 and LR5 releasing after class today

  • Last THA of the quarter!

Reminders

  • EDA report and code due by next Monday
  • THA2 final submissions by next Tuesday

2

3 of 12

Education Recap

Common plot mishaps:

    • Incorrect colors in bar_chart_high_school
    • Error bands in plot_hispanic_min_degree

Common conceptual mishaps:

    • OneHotEncoder ???
    • randomstate=42

3

4 of 12

This Time

  • Efficiency
  • Runtime analysis
  • Big-O

Last Time

  • Dissolve
  • How to join datasets

4

5 of 12

Method 1: Timing

In the reading, we looked at how to calculate the time it takes to run

  • This can be tricky to get right since many factors affect time it takes to run program

�Can still get a general idea of �how two algorithms “scale”�by looking at time it takes �to run.

5

6 of 12

Method 2: Count steps

Assumption: A single “simple” line of code takes the same amount of time to run in Python.

Examples:

num = 5 + 10;

print(“hello);

x > 5 and x % 2 == 0

6

num = 5 + 10

print(“hello”)

num > 5 and num % 2 == 0

  • Blocks of code
    • Sum of all the lines
  • For loops
    • Number of steps in body multiplied by number of times loop runs
  • Function call
    • The number of steps in its body

7 of 12

Counting example

7

def mystery(n: int) -> int:

x = n + 10

while n < 5:

x *= 2

n += 1

return x

mystery(1)

mystery(4)

8 of 12

Growth rate

We often think of algorithms in terms of growth rate in terms of the input or input data size (n)

Think of the runtime of: 5n3 + 4n2 + 3n + 7.

All terms seem significant when we look at smaller inputs.

However, what happens when n becomes extremely large? At that point the term with the largest power of n will dominate (n3).

We say it runs on the order of n3 or O(n3) (Big Oh of n cubed).

8

9 of 12

Quadratic

Comparison

9

10 of 12

Complexity classes

A category of algorithm efficiency based on the algorithm's relationship to the input size n

10

Class

Constant

If n doubles, then

Constant

O(1)

unchanged

Linear

O(n)

doubles

Quadratic

O(n2)

quadruples

Cubic

O(n3)

Multiplies by 8

...

Exponential

O(2n)

Multiplies drastically

11 of 12

Group Work:

Best Practices

When you first working with this group:

  • Introduce yourself!
  • If possible, angle one of your screens so that everyone can discuss together

Tips:

  • Starts with making sure everyone agrees to work on the same problem
  • Make sure everyone gets a chance to contribute!
  • Ask if everyone agrees and periodically ask each other questions!
  • Call TAs over for help if you need any!

11

12 of 12

Before Next Time

  • Complete Lesson 18
    • Remember not for points, but do go towards Checkpoint Tokens
  • THA 4 due tonight
  • EDA + LR 5 due on Monday

Next Time

  • NumPy
  • Images

12