CS Fundamentals: Scalability and Memory

  • View
    1.528

  • Download
    0

Embed Size (px)

Text of CS Fundamentals: Scalability and Memory

  • SCALABILITY AND MEMORYCS FUNDAMENTALS SERIES

    http://bit.ly/1TPJCe6

  • HOW DO YOU MEASURE AN ALGORITHM?

  • ???

  • CLOCK TIME?

  • CLOCK TIME?

  • DEPENDS ON WHOS COUNTING.

  • ALSO, TOO FLAKY EVEN ON THE SAME

    MACHINE.

  • THE NUMBER OF LINES?

  • THE NUMBER OF

    LINES?

  • THIS IS TWO LINES, BUT A WHOLE LOT OF STUPID.

  • THE NUMBER OF CPU CYCLES?

  • THE NUMBER OF

    CPU CYCLES?

  • DEPENDS ON THE RUNTIME.

  • ALL THESE METHODS SUCK.

    NONE OF THEM CAPTURE WHAT WE ACTUALLY CARE ABOUT.

  • ENTER BIG O!

  • TEXT

    ASYMPTOTIC ANALYSIS

  • TEXT

    ASYMPTOTIC ANALYSIS

    Big O is about asymptotic analysis

  • TEXT

    ASYMPTOTIC ANALYSIS

    Big O is about asymptotic analysis

    In other words, its about how an algorithm scales when the numbers get huge

  • TEXT

    ASYMPTOTIC ANALYSIS

    Big O is about asymptotic analysis

    In other words, its about how an algorithm scales when the numbers get huge

    You can also describe this as the rate of growth

  • TEXT

    ASYMPTOTIC ANALYSIS

    Big O is about asymptotic analysis

    In other words, its about how an algorithm scales when the numbers get huge

    You can also describe this as the rate of growth

    How fast do the numbers become unmanageable?

  • TEXT

    ASYMPTOTIC ANALYSIS

  • TEXT

    ASYMPTOTIC ANALYSIS

    Another way to think about this is:

  • TEXT

    ASYMPTOTIC ANALYSIS

    Another way to think about this is:

    What happens when your input size is 10,000,000? Will your program be able to resolve?

  • TEXT

    ASYMPTOTIC ANALYSIS

    Another way to think about this is:

    What happens when your input size is 10,000,000? Will your program be able to resolve?

    Its about scalability, not necessarily speed

  • TEXT

    PRINCIPLES OF BIG O

  • TEXT

    PRINCIPLES OF BIG O

    Big O is a kind of mathematical notation

  • TEXT

    PRINCIPLES OF BIG O

    Big O is a kind of mathematical notation

    In computer science, it means essentially means

  • TEXT

    PRINCIPLES OF BIG O

    Big O is a kind of mathematical notation

    In computer science, it means essentially means

    the asymptotic rate of growth

  • TEXT

    PRINCIPLES OF BIG O

    Big O is a kind of mathematical notation

    In computer science, it means essentially means

    the asymptotic rate of growth In other words, how does the running time of this function

    scale with the input size when the numbers get big?

  • TEXT

    PRINCIPLES OF BIG O

    Big O is a kind of mathematical notation

    In computer science, it means essentially means

    the asymptotic rate of growth In other words, how does the running time of this function

    scale with the input size when the numbers get big?

    Big O notation looks like this:

  • TEXT

    PRINCIPLES OF BIG O

    Big O is a kind of mathematical notation

    In computer science, it means essentially means

    the asymptotic rate of growth In other words, how does the running time of this function

    scale with the input size when the numbers get big?

    Big O notation looks like this:

    O(n) O(nlog(n)) O(n2)

  • TEXT

    PRINCIPLES OF BIG O

  • TEXT

    PRINCIPLES OF BIG O

    n here refers to the input size

  • TEXT

    PRINCIPLES OF BIG O

    n here refers to the input size

    Can be the size of an array, the length of a string, the number of bits in a number, etc.

  • TEXT

    PRINCIPLES OF BIG O

    n here refers to the input size

    Can be the size of an array, the length of a string, the number of bits in a number, etc.

    O(n) means the algorithm scales linearly with the input

  • TEXT

    PRINCIPLES OF BIG O

    n here refers to the input size

    Can be the size of an array, the length of a string, the number of bits in a number, etc.

    O(n) means the algorithm scales linearly with the input

    Think like a line (y = x)

  • TEXT

    PRINCIPLES OF BIG O

  • TEXT

    PRINCIPLES OF BIG O

    Scaling linearly can mean 1:1 (one iteration per extra input), but it doesnt necessarily

  • TEXT

    PRINCIPLES OF BIG O

    Scaling linearly can mean 1:1 (one iteration per extra input), but it doesnt necessarily

    It can simply mean k:1 where k is constant, like 3:1 or 5:1 (i.e., a constant amount of time per extra input)

  • TEXT

    PRINCIPLES OF BIG O

  • TEXT

    PRINCIPLES OF BIG O In Big O, we strip out any coefficients or smaller factors.

  • TEXT

    PRINCIPLES OF BIG O In Big O, we strip out any coefficients or smaller factors.

    The fastest-growing factor wins. This is also known as the dominant factor.

  • TEXT

    PRINCIPLES OF BIG O In Big O, we strip out any coefficients or smaller factors.

    The fastest-growing factor wins. This is also known as the dominant factor.

    Just think, when the numbers get huge, what dwarfs everything else?

  • TEXT

    PRINCIPLES OF BIG O In Big O, we strip out any coefficients or smaller factors.

    The fastest-growing factor wins. This is also known as the dominant factor.

    Just think, when the numbers get huge, what dwarfs everything else?

    O(5n) => O(n)

  • TEXT

    PRINCIPLES OF BIG O In Big O, we strip out any coefficients or smaller factors.

    The fastest-growing factor wins. This is also known as the dominant factor.

    Just think, when the numbers get huge, what dwarfs everything else?

    O(5n) => O(n)

    O(n - 10) also => O(n)

  • TEXT

    PRINCIPLES OF BIG O

  • TEXT

    PRINCIPLES OF BIG O

    O(k) where k is any constant reduces to O(1).

  • TEXT

    PRINCIPLES OF BIG O

    O(k) where k is any constant reduces to O(1).

    O(200) = O(1)

  • TEXT

    PRINCIPLES OF BIG O

    O(k) where k is any constant reduces to O(1).

    O(200) = O(1)

    Where there are multiple factors of growth, the most dominant one wins.

  • TEXT

    PRINCIPLES OF BIG O

    O(k) where k is any constant reduces to O(1).

    O(200) = O(1)

    Where there are multiple factors of growth, the most dominant one wins.

    O(n4 + n2 + 40n) = O(n4)

  • TEXT

    PRINCIPLES OF BIG O

  • TEXT

    PRINCIPLES OF BIG O

    If there are two inputs (say youre trying to find all the common substrings of two strings), then you use two variables in your Big O notation => O(n * m)

  • TEXT

    PRINCIPLES OF BIG O

    If there are two inputs (say youre trying to find all the common substrings of two strings), then you use two variables in your Big O notation => O(n * m)

    Doesnt matter if one variable probably dwarfs the other. You always include both.

  • TEXT

    PRINCIPLES OF BIG O

    If there are two inputs (say youre trying to find all the common substrings of two strings), then you use two variables in your Big O notation => O(n * m)

    Doesnt matter if one variable probably dwarfs the other. You always include both.

    O(n + m) => this is considered linear

  • TEXT

    PRINCIPLES OF BIG O

    If there are two inputs (say youre trying to find all the common substrings of two strings), then you use two variables in your Big O notation => O(n * m)

    Doesnt matter if one variable probably dwarfs the other. You always include both.

    O(n + m) => this is considered linear

    O(2n + log(m)) => this is considered exponential

  • TEXT

    COMPREHENSION TEST

  • TEXT

    COMPREHENSION TEST

    Convert each of these to their appropriate Big O form!

  • TEXT

    COMPREHENSION TEST

    Convert each of these to their appropriate Big O form!

    O(3n + 5)

  • TEXT

    COMPREHENSION TEST

    Convert each of these to their appropriate Big O form!

    O(3n + 5)

    O(n + 1/5n2)

  • TEXT

    COMPREHENSION TEST

    Convert each of these to their appropriate Big O form!

    O(3n + 5)

    O(n + 1/5n2)

    O(log(n) + 5000)

  • TEXT

    COMPREHENSION TEST

    Convert each of these to their appropriate Big O form!

    O(3n + 5)

    O(n + 1/5n2)

    O(log(n) + 5000)

    O(2m3 + 50 + n)

  • TEXT

    COMPREHENSION TEST

    Convert each of these to their appropriate Big O form!

    O(3n + 5)

    O(n + 1/5n2)

    O(log(n) + 5000)

    O(2m3 + 50 + n)

    O(nlog(m) + 2m2 + nm)

  • What should n be for this function?

  • For each character in the string

    Unshift them into an arrayAnd then join the array together.

    Lets break it down.

    Make an empty array.

  • For each character in the string

    Unshift them into an arrayAnd then join the array together.

    Initialize an empty array => O(1)

    Then, split the string into an array of characters => O(n)

    Then for each character => O(n)

    Unshift into an array => O(n)

    Then join the characters into a string => O(n)

    Well see later why this is.

    Make an empty array.

  • For each character in the string

    Unshift them into an arrayAnd then join the array together.

    Initialize an empty array => O(1)

    Then, split the string into an array of characters => O(n)

    Then for each character =>