Big-O¶
Learning objectives
- Understand misconceptions on optimisation
- Understand Amdahl's law
- Practice to determine the big-O profile of a function
- Practice to get a run-time speed profile
QMD¶
Problem¶
Q: When to optimize for speed?
A:
- C++ Core Guidelines: Per.1: Don't optimize without reason
- C++ Core Guidelines: Per.2: Don't optimize prematurely
- C++ Core Guidelines: Per.3: Don't optimize something that's not performance critical
Problem¶
Q: How to improve the run-time speed of an algorithm?
. . .
Make it work, make it right, make it fast.
Kent Beck
A (simplified):
- Measure (hard to do @bartz2020benchmarking)
- Think
- Change code
- Measure again
Problem¶
Q: How to improve the run-time speed of an algorithm?
A (simplified):
- Measure big-O
- Measure speed profile
- Think
- Change code
- Measure again
Measurement 1: big-O¶
How your (combination of) algorithms scales with more complex input.
- Counting the words in a book: O(n)
- Looking up a word in a dictionary: O(log2(n))
Do measure big-O in release mode!
Your algorithm¶
Example¶
:monocle_face: Work on B?
Example¶
Example¶
No, work on C instead
Discussion¶
Big-O helps to:
- find algorithm to profile
- make predictions
Agree yes/no
Exercise 1 [SKIP]¶
- Measure big-O complexity of https://www.pythonpool.com/check-if-number-is-prime-in-python/
def isprime(num):
if num> 1:
for n in range(2,num):
if (num % n) == 0:
return False
return True
else:
return False
Exercise 1 [SKIP]¶
- Measure big-O complexity of https://www.pythonpool.com/check-if-number-is-prime-in-python/
def Prime(no, i = 2):
if no == i:
return True
elif no % i == 0:
return False
return Prime(no, i + 1)
Exercise 2 [SKIP]¶
- Measure big-O complexity of DNA alignment at https://johnlekberg.com/blog/2020-10-25-seq-align.html