Code efficiency in this example


#1

Hi folks. My print lines in this example are:

print get_class_average(students)
print get_letter_grade(get_class_average(students))

My question is - would it be more efficient to write

student_class_average = get_class_average(students)
print student_class_average
print get_letter_grade(student_class_average)

or is it there a difference? I'm thinking technically in the first example I'm evaluating get_class_average twice, in much more complex programs that would require more memory.


#2

In this script, it does not make a difference because get_class_average function is really simple and our list of students is very short.

Of course, you are right and the second code is much better than the first one. Even if this does not make a difference now - it's a really good habit.

In your example, we should not care about space (memory), but about the time of the execution, the number of operations. I don't know if you are familiar with big O notation, we use it to represent (growth of) time and space complexity of algorithms. Even when we deal with an O(n) (linear) algorithm, like calculating a sum of the elements in the array, if there are many elements - it takes time. So if we have to use the result of our algorithm twice - we have to save the result and use created variable like you did in the second example.


I want to give one more example. Let's say that we have a fib(n) functions that returns the n-th member of the Fibonacci sequence.

def fib(n):
    if n <= 1: return n
    return fib(n - 1) + fib(n - 2)

Our work is to create an interface, bridge between function and user. We have to ask user for number and then we have to give him output - result of fib(number).

So user comes to us and says that he would like to get the 10-th number, so our program calculates:

fib(10) =
fib(9) + fib(8) =
fib(8) + fib(7) + fib(8) =
fib(7) + fib(6) + fib(7) + fib(8) =
fib(6) + fib(5) + fib(6) + fib(7) + fib(8) =
fib(5) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
fib(4) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
fib(3) + fib(2) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
fib(2) + fib(1) + fib(2) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
fib(1) + fib(0) + fib(1) + fib(2) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
1 + fib(0) + fib(1) + fib(2) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
1 + fib(1) + fib(2) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
2 + fib(2) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
2 + fib(1) + fib(0) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
3 + fib(0) + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
3 + fib(3) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
3 + fib(2) + fib(1) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
3 + fib(1) + fib(0) + fib(1) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
4 + fib(0) + fib(1) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
4 + fib(1) + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
5 + fib(4) + fib(5) + fib(6) + fib(7) + fib(8) =
(...)

Ok, we don't even have a fib(9) value yet. You should notice that there are many repeating function calls in calculations above, for example - there are 18 fib(5) calls. Every call will be calculated separately.

We calculate the same values many, many times. Nightmare, right?

But ok, we have a very fast computer so it outputs 55 and we give the result to our happy customer. But next person asks about fib(12). And we have to calculate it the same way like fib(10). Even if now we know fib(10 and fib(9) values.

Solution to this problem is very simple. We can change our algorithm to iterative version and we can create a global array that will store calculated values.

What I wanted to say is - I am really happy that you asked this question. Your simple idea to memorize value is a base of great method to solve problems - dynamic programming. I hope that you will became a great programmer! :smile:


#3

Thanks for the reply. I was a CS major in the late 90s early 00s but I've been an infrastructure guy ever since. Codecademy is helping me get back into development! Things big O notation definitely rings some bells but some things like that are stored away in dark cobweb filled places in my mind. Thanks for taking time on the response and shining some light in those dark corners. :smile:


#4

Oh, I'm studying computer science! :smile:

I hope that you will achieve your goal.

It was a pleasure to answer your question.