CSE 163
Profiling
��Hunter Schafer
💬Before class: Do you have any thoughts yet on what you want to look into for your final project?
🎵Music: Tom Misch
Max Diff
What are the Big-O run-times of each of these methods?
2
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)
Big-O
3
Why is Python Slow?
Interpreter
Dynamically types
4
Example: Arithmetic
Consider this C program
This roughly maps to the machine instructions
5
/* 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
6
# A Python program
a = 1
b = 2
c = a + b
Example: Arithmetic
7
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
8
Project Group Finding
9
Instructions
��
10