Mit6 100l f22 Lec21
Mit6 100l f22 Lec21
Mit6 100l f22 Lec21
COUNTING OPERATIONS
(download slides and .py files to follow along)
6.100L Lecture 21
Ana Bell
1
WRITING EFFICIENT PROGRAMS
6.100L Lecture 21
EFFICIENCY IS IMPORTANT
6.100L Lecture 21
EVALUATING PROGRAMS
6.100L Lecture 21
ASIDE on MODULES
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
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
6.100L Lecture 21
TIMING mysum
6.100L Lecture 21
TIMING square
10
6.100L Lecture 21
TIMING PROGRAMS IS
INCONSISTENT
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
6.100L Lecture 21
COUNTING c_to_f
14
6.100L Lecture 21
COUNTING mysum
15
6.100L Lecture 21
COUNTING square
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
6.100L
6.0001Lecture
LECTURE 821
… STILL NEED A BETTER WAY
18
6.100L Lecture 21
MITOpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms ofUse,visit: https://ocw.mit.edu/terms.
19