cse 250 lecture 3

25
Fall 2021 ©Oliver Kennedy, Andrew Hughes The University at Buffalo, SUNY 1 / 25 CSE 250 Lecture 3 Function Analysis Textbook Ch. 7.3-7.4

Upload: others

Post on 30-Jan-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 1 / 25

CSE 250Lecture 3Function AnalysisTextbook Ch. 7.3-7.4

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 2 / 25

What is “Fast”?

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 3 / 25

When is an algorithm “fast”?

● Wall-clock time?

– Is 10s fast? Is 100ms fast? Is 10µs fast?● It depends on the task

– Algorithm vs Implementation.● Compare Grace Hopper’s implementation vs yours

– CPU: ARM iMX6 vs Intel i9 vs AMD 5950● Different speed/capability trade-offs

– Bottlenecks: CPU vs IO vs Memory vs Network vs …

Wall-clock time isn’t great.

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 4 / 25

When is an algorithm “fast”?

● Take into account the “scale” of the problem

– Consider the number of input records, users, pixels, etc..

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 5 / 25

Scaling

● “Five steps plus Ten steps per user”

– 5 + ( 10 x |Users| )● “Ten Steps per connection. Each user has connections to 1% of the other

users in the system.”

– 10 x ( |Users| x ( 0.01 x |Users| ) )● “Seven steps for every possible combination of users.”

– 7 x ( 2|Users| )● “For each user: ten steps plus three per post”

– |Users| x ( 10 + 3 x |Posts| )

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 6 / 25

When is an algorithm “fast”?

● Take into account the “scale” of the problem

– Define the number of steps as a function of the input size.

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 7 / 25

Functions

0 1 2 3 4 5 6 7

-4

1

6

11

16

21

26

log(x)

x

x^2

x!

Number of Users

Ru

ntim

e

Would you use an algorithm that takes |Users|! steps?

Maybe?No

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 8 / 25

Functions

0 1 2 3 4 5 6 70

10

20

30

40

50

60

3x+5

x^2

Number of Users

Ru

ntim

e

Which is better? 3|Users|+5 steps or |Users|2 steps?

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 9 / 25

When is an algorithm “fast”?

● Take into account the “scale” of the problem

– Define the number of steps as a function of the input size.● Focus on “large” inputs

– Rank functions based on how they behave at large scales.

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 10 / 25

What hardware are you using?

vs

Intel i9

Images from openclipart.org, used with permission

Motorola 68000

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 11 / 25

Who implemented the algorithm?

vs

Epic-Level Implementation Error 23: Cat on Keyboard

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 12 / 25

Goal: Judge the Algorithm Not the Implementation or Deployment

● How fast is a step?

– Only count number of steps● Can this be done in two steps instead of one?

– “3 steps per user” vs “some number of steps per user”

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 13 / 25

When is an algorithm “fast”?

● Take into account the “scale” of the problem.

– Define the number of steps as a function of the input size.● Focus on “large” inputs.

– Rank functions based on how they behave at large scales.● Decouple from infrastructure / implementation.

– Asymptotic Notation

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 14 / 25

Logarithms

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 15 / 25

Logarithms (refresher)

If no base specified, assume base-2(in this class)

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 16 / 25

Logarithms (refresher)

● Let● Exponent rule:● Product rule:● Division rule:● Change of base from b to c:

– Base changes are only a constant factor off● Log/Exponent are inverses:

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 17 / 25

Growth Functions

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 18 / 25

Growth Functions

A growth function must be a non-decreasing function of the form

(non-negative integers) (positive real numbers)

f is a function from ... … to …

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 19 / 25

Behavior at Scale

Two functions of n grow “the same” if they behave the same when n is large.

vs

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 20 / 25

Behavior at Scale

1 4 16 64 256

1024

4096

1638

4

6553

61

100

10000

1000000

100000000

10000000000

1000000000000

100000000000000

1E+016

N^3

(1/100)N^3 + ...

1 2 4 8 16 32 64 128

256

512

1024

2048

4096

8192

1638

4

3276

8

6553

6

1310

721

100

10000

1000000

100000000

10000000000

1000000000000

100000000000000

1E+016

N^3

(1/100)N^3 + ...

~100x difference

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 21 / 25

Behavior At Scale

0 0

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 22 / 25

Asymptotic Analysis @ 5k ft

● Consider: vs

● Case 1:

– grows faster, is better

● Case 2:

– grows faster, is better

● Otherwise:

– Both “behave the same”

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 23 / 25

Asymptotic Analysis @ 5k ft

Goal: Before you start optimizing the constantsFigure out if you’re in a different function class.

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 24 / 25

Another Example

Remember: exponential polynomial log constant≫ ≫ ≫

Fall 2021 ©Oliver Kennedy, Andrew HughesThe University at Buffalo, SUNY 25 / 25

Why focus on dominating terms?

10 20 50 100 1000

0.43 ns 0.52 ns 0.62 ns 0.68 ns 0.82 ns

0.83 ns 1.01 ns 1.41 ns 1.66 ns 2.49 ns

2.5 ns 5 ns 12.5 ns 25 ns 0.25 µs

8.3 ns 22 ns 71 ns 0.17 µs 2.49 µs

25 ns 0.1 µs 0.63 µs 2.5 µs 0.25 ms

25 µs 0.8 ms 78 ms 2.5 s 2.9 days

0.25 µs 0.26 ms 3.26 days 1013 years 10284 years

0.91 ms 19 years 1047 years 10141 years 🤯

Time to execute instructions on a 4GHz Processor