1 of 10

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

2 of 10

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)

3 of 10

Big-O

  • In theory, Big-O is a great descriptor, but in practice we generally also include timing information to help us reason about our program efficiency
  • There is not one right tool, they both have their place!
    • Big-O
      • Helps us communicate how our algorithm scales
      • Easily eliminate algorithms that won’t scale
    • Timing
      • Verify our implementation is fast
      • Help us find bottlenecks in our algorithm

3

4 of 10

Why is Python Slow?

Interpreter

  • We have mentioned before that when we are using Python, we are using an interpreter rather than a compiler
  • This means it has to figure how to translate your code while it is running
  • This can be overly slow in “hot” loops

Dynamically types

  • We don’t have to write down the types ahead of time, which means Python spends a fair bit of time figuring out which type each object is

4

5 of 10

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

6 of 10

Example: Arithmetic

Consider this Python program

This roughly maps to the machine instructions

6

# A Python program

a = 1

b = 2

c = a + b

7 of 10

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

8 of 10

Slow Python

  • If Python is so slow, why does anyone use it?
    • It is WAY easier to program with than a language like C
      • Developer time costs $$$, computers are cheap
    • Python provides incredible support for plugging into pre-compiled libraries
      • Examples: pandas, sklearn, numpy, scipy
  • Pretty easy to get around this
    • Use a profiler to figure out where your bottlenecks are
    • If your program is slow, look to any “hot” loops that might be replaced with library calls.

8

9 of 10

Project Group Finding

9

10 of 10

Instructions

  • See the course Ed Chat “Project-Finding” thread.
  • There are sub-threads for various topics.
  • Join a thread you are interested in and send a message describing what you’re interested in working on at a high level!
  • If you see a topic you want to work on, start a thread to let that person know and chat with them about some more ideas!

��

  • Staff will be on board to help facilitate
  • You can also DM someone contact details so you can message them later!

10