Over a million developers have joined DZone.

Thinking Performance in Programming

DZone 's Guide to

Thinking Performance in Programming

Creating code that doesn't scale properly and can't handle the big issues is a result of programming without thinking about performance first.

· Performance Zone ·
Free Resource

Image title

Image title

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 problem, or do we just feel comfortable with “My code works?" Don’t stop there, take your thinking bigger than “My code works” to “Can my code solve a huge problem?”  as 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 about the thought you put into your coding.

We can only think performance when we think Big O, as in Big O Notation. Big O Notation simply means describing the performance or complexity of an algorithm. Big O Notation deals with solving performance issues. I strongly recommend you read up on with this simplified guide. 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)

Ideally, we have solved the problem with a few lines of code but does that mean this code will work well with a huge number? Let's 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 three different ways we can solve this problem and choose the best that performs well.

Image title

When we take a look at the image above, we can see that it took fifteen seconds to solve the problem, which means it's 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.

Image title

Taking a look at the image above also we can see it took it five seconds to solve the same problem, which in my opinion is better, but it still doesn't look like the best solution to me. Finally, let us take a look at the third solution.

Image title

Wow, this is really amazing, this solution solved the problem in approximately 0.00002 seconds. What a wide margin compared to the 15- and 5-second solutions. Now this is what I mean by thinking about 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.

programming ,performace ,python ,scaling

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}