CSE 163
Performance + Profiling
Hunter Schafer
Method 2: Count steps
Assumption: A single simple line of code takes the same amount of time to run in Python.
Examples:
num = 5 + 10;
print(βhello);
x > 5 and x % 2 == 0
Rules
2
num = 5 + 10
print(βhelloβ)
num > 5 and num % 2 == 0
Growth rate
We often think of algorithms in terms of growth rate in terms of the input or input data size (n)
Think of the runtime of 5n3 + 4n2 + 3n + 7.
All terms seem significant when we look at smaller inputs.
However, what happens when n becomes extremely large? At that point the term with the largest power of n will dominate (n3).
We say it runs on the order of n3 or O(n3) (Big Oh of n cubed).
3
Complexity classes
4
http://recursive-design.com/blog/2010/12/07/comp-sci-101-big-o-notation/ - post about a Google interview
1 min - Review
What are the Big-O run-times of each of these methods?
5
def max_diff1(nums):
max_diff = 0
for n1 in nums:
for n2 in nums:
diff = abs(n1 - n2)
if diff > max_diff:
max_diff = diff
return max_diff
def max_diff2(nums):
min_num = nums[0]
max_num = nums[0]
for num in nums:
if num < min_num:
min_num = num
elif num > max_num:
max_num = num
return max_num - min_num
def max_diff3(nums):
return max(nums) - min(nums)
Think
pollev.com/cse163
1.5 min - Review
What are the Big-O run-times of each of these methods?
What are the Big-O run-times of each of these methods?
6
def max_diff1(nums):
max_diff = 0
for n1 in nums:
for n2 in nums:
diff = abs(n1 - n2)
if diff > max_diff:
max_diff = diff
return max_diff
def max_diff2(nums):
min_num = nums[0]
max_num = nums[0]
for num in nums:
if num < min_num:
min_num = num
elif num > max_num:
max_num = num
return max_num - min_num
def max_diff3(nums):
return max(nums) - min(nums)
Pair
pollev.com/cse163
Big-O
Pros
Cons
7
Matrix Multiplication
8
Big-O
9
1 min
Rank the algorithms in term of their speed on my computer
β1=2 < 3β means 1 and 2 are about the same, but faster than 3
10
def max_diff1(nums):
max_diff = 0
for n1 in nums:
for n2 in nums:
diff = abs(n1 - n2)
if diff > max_diff:
max_diff = diff
return max_diff
def max_diff2(nums):
min_num = nums[0]
max_num = nums[0]
for num in nums:
if num < min_num:
min_num = num
elif num > max_num:
max_num = num
return max_num - min_num
def max_diff3(nums):
return max(nums) - min(nums)
Think
pollev.com/cse163
1 min
Rank the algorithms in term of their speed on my computer
β1=2 < 3β means 1 and 2 are about the same, but faster than 3
11
def max_diff1(nums):
max_diff = 0
for n1 in nums:
for n2 in nums:
diff = abs(n1 - n2)
if diff > max_diff:
max_diff = diff
return max_diff
def max_diff2(nums):
min_num = nums[0]
max_num = nums[0]
for num in nums:
if num < min_num:
min_num = num
elif num > max_num:
max_num = num
return max_num - min_num
def max_diff3(nums):
return max(nums) - min(nums)
Pair
pollev.com/cse163
Timing
12
conda install line_profiler
kernprof -v -l file.py
Why is Python Slow?
Interpreter
Dynamically types
13
Example: Arithmetic
Consider this C program
This roughly maps to the machine instructions
14
/* C code */
int a = 1;
int b = 2;
int c = a + b;
Assign <int> 1 to a
Assign <int> 2 to b
call add<int, int>(a, b)
Assign <int> the result to c
Example: Arithmetic
Consider this Python program
This roughly maps to the machine instructions
15
# A Python program
a = 1
b = 2
c = a + b
Example: Arithmetic
16
1. Assign 1 to a
1a. Set a->PyObject_HEAD->typecode to integer
1b. Set a->val = 1
2. Assign 2 to b
2a. Set b->PyObject_HEAD->typecode to integer
2b. Set b->val = 2
3. call add(a, b)
3a. find typecode in a->PyObject_HEAD
3b. a is an integer; value is a->val
3c. find typecode in b->PyObject_HEAD
3d. b is an integer; value is b->val
3e. call add<int, int>(a->val, b->val)
3f. result of this is result, and is an integer.
4. Create a Python object c
4a. set c->PyObject_HEAD->typecode to integer
4b. set c->val to result
Slow Python
17
Group Finding
18
19
Activity
TAs will give instructions
Share
Walk Around
20