DEV Community

Cover image for Thinking Performance in Programming
t4christ
t4christ

Posted on

Thinking Performance in Programming

As a programmer we have written codes that ideally solves a problem, but do we take note of the performance when it comes to solving a huge size of the problem or do we just feel comfortable with "MY CODE WORKS". Well we can say as far as my code works then that is all. Don't stop there, take your thinking bigger than "MY CODE WORKS" to "CAN MY CODE SOLVE A HUGE SIZE OF PROBLEM" this is where thinking on a performance level comes in. For those who are not python programmers this will work for you too because it's more of the thought you put into your coding.

We can only think performance when we think Big O. Hahahaha you wonder what i mean by "THINK BIG O" right? Not to worry all am saying is Big O Notation.Big O Notation simply means describing the performance or complexity of an algorithm. Big O Notation deals with solving performance issues, the big performance issue is where big o comes into the picture. I will strongly recommend you read on big o notation with this simplified guide https://guide.freecodecamp.org/computer-science/notation/big-o-notation/. Enough of the theory let's see what i mean in practice. We will be looking at a simple mathematical problem in python. Let us assume we want to sum up all numbers from 1 to 100 with python we can simply do this.

def sumNumber(number):
sum = 0
for num in number: ## Loop through an array of numbers
sum +=num ## add number to initial sum value
return sum ## return sum after the loop
number = range(100)
sumNumber(number)

Output:
4950

Ideally we have solved the problem with this few lines of code but does that mean this code will work well with a huge number. Lets take a look at the performance of this code when it comes to solving the same problem with one hundred million (100,000,000). Let's take a look at 3 different ways we can solve this problem and choose the best that performs well.

Solution 1

import timeit

def sumNumber(number):
sum = 0
for n in number:
sum+=n
return sum

number = range(100)
start_time = timeit.default_timer()
result = sumNumber(number)
print(result,timeit.default_timer() - start_time)

When we take a look at the solution above it took 15 seconds to solve the problem this is not really a good solution considering the size.

Let us take a look at the second way of solving the same problem with the same size using python's built in sum method.

Solution 2

import timeit

def sumNumber(number):
return sum(number)

number = range(100000000)
start_time = timeit.default_timer()
sumNumber(number)
print(timeit.default_timer() - start_time)

Taking a look at solution 2 above it took it 5 seconds to solve the same problem which in my opinion I will say is better but it still dosen't look like the best solution to me. We can still make it better don't you think. Finally let us take a look at the third solution.

Solution 3

import timeit

def sumNumber(number):
return number/2 *(1+number)

number = 10000000000
start_time = timeit.default_timer()
sumNumber(number)
print(timeit.default_timer() - start_time)

Wow I can't believe what my eyes are seeing. This is really amazing, this solution solved the problem in approximately 0.00002 seconds. What a wide margin compared to the 15 and 5 seconds solution. Now this is what I mean by thinking performance when it comes to coding. No matter the problem you want to solve with your programming language of choice you can always think performance and you will be amazed by the results. Thank you so much for following along. Kindly drop your comments if you have any questions or addition.

Top comments (0)