How can we verify the performance of an algorithm?

willianmikhael

Willian Mikhael

Posted on February 20, 2024

How can we verify the performance of an algorithm?

I will be brief in the explanation

Let's start talking about what is an algorithm;

In short:
An algorithm is a set of instructions that perform a task.

So, How can we verify the performance of an algorithm?

We can use Big O Notation. This is a special notation that tells you how fast an algorithm is.

So, lets look at an example:

Imagine that, you have to do a 16 divisions on a paper, below we have two algorithms, lets check which is better:

1- One way to draw this 16-division grid is to draw a division of
each time. Remember, Big O notation counts the number of operations. In that For example, drawing a division is an operation. You need to draw 16 divisions. How many operations will you have to do if you draw a division by turn?

Image description

You need to go through 16 steps to draw 16 divisions.

2- Try this algorithm now. Fold the paper.
In this example, folding the paper once is one operation. You did two divisions with this operation!

Image description

Fold the paper again and again and again
Unfold after four folds and you have a beautiful grid! With each fold, the number of divisions doubles. You made 16 divisions with four operations!
You can “draw” twice as many divisions with each fold, so you
can draw 16 divisions in four steps.

What is the execution time for this algorithm?

Answers: Algorithm 1 has O(n) execution time and algorithm 2 has
O(log n) execution time.

Now let's understand the Big O Notation

Big O notation uses a capital letter “O” followed by a mathematical expression that represents the algorithm's execution time in relation to the size of the input.

You may not remember logarithms, but you probably remember how to calculate exponentials. The expression log10 100 basically says: “How many 10s can we multiply to get to 100?” The answer is 2: 10 × 10. So log10 100 = 2.

Image description

Logarithms are the opposite of exponentials.

Some common runtime examples Big O

Here we have five Big O runtimes that you'll find plenty of, ordered from fastest to slowest.

• O(log n), also known as logarithmic time.
• O(n), known as linear time.
• O(n * log n).
• O(n2).
• O(n!).

Note

Big O notation takes into account the worst case scenario, but we can also analyze the "average case"

This is a very simple and superficial explanation, but it can give you an introduction to the subject. These examples are taken from the book Grokking Algorithms by Aditya Y. Bhargava

💖 💪 🙅 🚩
willianmikhael
Willian Mikhael

Posted on February 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related