0% found this document useful (0 votes)
5 views

Lecture 1.9 - Timing Our Code

Uploaded by

smitstudying
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 1.9 - Timing Our Code

Uploaded by

smitstudying
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Timing our code

Madhavan Mukund
https://www.cmi.ac.in/~madhavan

Programming, Data Structures and Algorithms using Python


Week 1
Timing our code
How long does our code take to
execute?
Depends from one language to
another

Madhavan Mukund Timing our code PDSA using Python Week 1 2/3
Timing our code
How long does our code take to
execute?
Depends from one language to
another

Python has a library time with various


useful functions

Madhavan Mukund Timing our code PDSA using Python Week 1 2/3
Timing our code
How long does our code take to
execute?
Depends from one language to
another

Python has a library time with various


useful functions
perf time() is a performance counter
Absolute value of perf time() is not
meaningful
Compare two consecutive readings to
get an interval
Default unit is seconds

Madhavan Mukund Timing our code PDSA using Python Week 1 2/3
Timing our code
How long does our code take to
execute? import time
Depends from one language to
another start = time.perf_counter()
...
Python has a library time with various # Execute some code
useful functions ...
perf time() is a performance counter end = time.perf_counter()
Absolute value of perf time() is not
elapsed = end - start
meaningful
Compare two consecutive readings to
get an interval
Default unit is seconds

Madhavan Mukund Timing our code PDSA using Python Week 1 2/3
A timer object
import time
Create a timer class
class Timer:

Madhavan Mukund Timing our code PDSA using Python Week 1 3/3
A timer object
import time
Create a timer class
class Timer:
Two internal values
def __init__(self):
start time self._start_time = 0
elapsed time self._elapsed_time = 0

Madhavan Mukund Timing our code PDSA using Python Week 1 3/3
A timer object
import time
Create a timer class
class Timer:
Two internal values
def __init__(self):
start time self._start_time = 0
elapsed time self._elapsed_time = 0
start starts the timer def start(self):
self._start_time = time.perf_counter()

Madhavan Mukund Timing our code PDSA using Python Week 1 3/3
A timer object
import time
Create a timer class
class Timer:
Two internal values
def __init__(self):
start time self._start_time = 0
elapsed time self._elapsed_time = 0
start starts the timer def start(self):
self._start_time = time.perf_counter()
stop records the
elapsed time def stop(self):
self._elapsed_time =
time.perf_counter() - self._start_time
def elapsed(self):
return(self._elapsed_time)

Madhavan Mukund Timing our code PDSA using Python Week 1 3/3
A timer object
import time
Create a timer class
class Timer:
Two internal values
def __init__(self):
start time self._start_time = 0
elapsed time self._elapsed_time = 0
start starts the timer def start(self):
self._start_time = time.perf_counter()
stop records the
elapsed time def stop(self):
self._elapsed_time =
More sophisticated time.perf_counter() - self._start_time
version in the actual
code def elapsed(self):
return(self._elapsed_time)

Madhavan Mukund Timing our code PDSA using Python Week 1 3/3
A timer object
import time
Create a timer class
class Timer:
Two internal values
def __init__(self):
start time self._start_time = 0
elapsed time self._elapsed_time = 0
start starts the timer def start(self):
self._start_time = time.perf_counter()
stop records the
elapsed time def stop(self):
self._elapsed_time =
More sophisticated time.perf_counter() - self._start_time
version in the actual
code def elapsed(self):
return(self._elapsed_time)
Python executes 107
operations per second
Madhavan Mukund Timing our code PDSA using Python Week 1 3/3

You might also like