Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Mit6 100l f22 Lec21

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

TIMING PROGRAMS,

COUNTING OPERATIONS
(download slides and .py files to follow along)
6.100L Lecture 21
Ana Bell

1
WRITING EFFICIENT PROGRAMS

 So far, we have emphasized correctness. It is the first


thing to worry about! But sometimes that is not enough.
 Problems can be very complex
 But data sets can be
very large: in 2014
Google served
30,000,000,000,000
pages covering
100,000,000 GB
of data
2

6.100L Lecture 21
EFFICIENCY IS IMPORTANT

 Separate time and space efficiency of a program


 Tradeoff between them: can use up a bit more memory
to store values for quicker lookup later
 Think Fibonacci recursive vs. Fibonacci with memoization

 Challenges in understanding efficiency


 A program can be implemented in many different ways
 You can solve a problem using only a handful of different
algorithms
 Want to separate choice of implementation from choice
of more abstract algorithm
3

6.100L Lecture 21
EVALUATING PROGRAMS

 Measure with a timer


 Count the operations
 Abstract notion of order of growth

6.100L Lecture 21
ASIDE on MODULES

 A module is a set of python definitions in a file


 Python provides many useful modules: math, plotting/graphing, random
sampling for probability, statistical tools, many others
 You first need to “import” the module into your environment
import time
import random
import dateutil
import math

 Call functions from inside the module using the module’s name
and dot notation
math.sin(math.pi/2)

6.100L
6.0001Lecture
LECTURE 921
TIMING

6.100L Lecture 21
TIMING A PROGRAM

 Use time module import time


 Recall that
importing means to def c_to_f(c):
bring in that class return c*9.0/5 + 32
into your own file

tstart = time.time()
 Start clock
c_to_f(37)
 Call function
dt = time.time() - tstart
 Stop clock
print(dt, "s,")

6.100L Lecture 21
TIMNG c_to_f

 Very fast, can’t even time it accurately

6.100L Lecture 21
TIMING mysum

 As the input increases, the time it takes also increases


 Pattern?
 0.009 to 0.05 to 0.5 to 5 to ??

6.100L Lecture 21
TIMING square

 As the input increases the time it takes also increases


 square called with 100000 did not finish within a reasonable
amount of time
 Maybe we can guess a pattern if we are patient for one more
round?

10

6.100L Lecture 21
TIMING PROGRAMS IS
INCONSISTENT

 GOAL: to evaluate different algorithms


 Running time should vary between algorithms
 Running time should not vary between implementations
 Running time should not vary between computers
 Running time should not vary between languages
Running time is should be predictable for small inputs

 Time varies for different inputs but


cannot really express a relationship
between inputs and time needed
Can only be measured a posteriori
11

6.100L
6.0001Lecture
LECTURE 821
COUNTING

12

6.100L Lecture 21
COUNTING c_to_f  3 ops
def c_to_f(c):
OPERATIONS return c*9.0/5 + 32

mysum  1+(x+1)*(1+2) = 3x+4 ops


 Assume these steps take def mysum(x):
constant time: total = 0
• Mathematical operations for i in range(x+1):
• Comparisons total += i
• Assignments return total
• Accessing objects in memory
 Count number of
square 1+n*(1)*n*(1+2) = 3n2 + 1 ops
operations executed as def square(n):
function of size of input sqsum = 0
for i in range(n):
for j in range(n):
sqsum += 1
return sqsum
13

6.100L Lecture 21
COUNTING c_to_f

 No matter what the input is, the number of operations is the


same

14

6.100L Lecture 21
COUNTING mysum

 As the input increases by 10, the number if operations ran is


approx. 10 times more.

15

6.100L Lecture 21
COUNTING square

 As the input increases


by 10, the number of
operations is approx.
100 times more.

 As the input increases


by 2, the number of
operations is approx.
4 times more.

16

6.100L Lecture 21
COUNTING OPERATIONS IS
INDEPENDENT OF COMPUTER
VARIATIONS, BUT …
 GOAL: to evaluate different algorithms
 Running “time” should vary between algorithms
 Running “time” should not vary between implementations
 Running “time” should not vary between computers
 Running “time” should not vary between languages
 Running “time” is should be predictable for small inputs
 No real definition of which operations to count

 Count varies for different inputs and


can derive a relationship
between inputs and the count 17

6.100L
6.0001Lecture
LECTURE 821
… STILL NEED A BETTER WAY

• Timing and counting evaluate implementations


• Timing and counting evaluate machines

• Want to evaluate algorithm


• Want to evaluate scalability
• Want to evaluate in terms of input size

18

6.100L Lecture 21
MITOpenCourseWare
https://ocw.mit.edu

6.100L Introduction to Computer Science and Programming Using Python


Fall 2022

For information about citing these materials or our Terms ofUse,visit: https://ocw.mit.edu/terms.

19

You might also like