Wednesday, 1 April 2015

Week 12: Efficiency and Final Comments

This week, we finished off a basic introduction to the principles of analysis regarding code efficiency. In particular, we learned how to classify the order of the runtime using 'big-O' notation. What this basically means is that no matter what size we are given, the runtime is proportional to some order based on the input size. We provide several examples to illustrate this point:

Ex 1) Single Loop

total = 0
for i in range(5000):
    total += 1
print(total)

Here the order is constant, denoted by O(I) since the code does not depend on any input values. Each time the code runs, it performs exactly 5002 operations (1 for line 1, 5000 for the for loop, and 1 for the print).

Ex 2) Two Loops

total = 0:
for i in range(n):
    for j in range(m):
        total += 1
print(total)

Here the order depends on both the sizes of two inputs, m and n, so the order is O(m*n). We know this because the nested for loop runs a total of n*m times. In the analysis of 'big-O' notation, we can disregard constants such as the first line and the print statement since O(m*n) can simply be multiplied by a constant to get the desired bound.

Ex 3) Branching Code

total = 0:
if (n % 2 == 0):
    for i in range(n**2)
        total += 1
else:
    for i in range(5, n + 3):
        total += 1
print(total)

Clearly, the order for the 'if' clause is O(n**2) and the order for the 'else' clause is O(n). In these cases, we take the worst case scenario as our official order since the worst case will always suffice as a bound for our analysis.

In sum, this basic analysis gives us a better understanding of how efficient our code is especially in cases where the input becomes arbitrarily large.

Final Comments: I am very satisfied with this course and feel I have learned a lot despite all the challenges. I hope to practice a lot before the final and showcase all that I have worked so hard to achieve. I look forward to taking further courses in computer science and expanding my horizons in this discipline.

No comments:

Post a Comment