DEV Community

xh1m
xh1m

Posted on

Why Your Code is Slow: A Practical Guide to Algorithms and Big O Notation

If you’re a Software Development student and they’ve given you the Algorithms unit to work on, then you’re probably having a problem by now. It’s not enough to simply get your code working to pass a unit assessment. You may have a simple CRUD application that works fine with ten records in your Oracle database. But as you add more records, your application slows down. It’s not a hardware problem; it’s a problem of your code getting more expensive to run. In other words, it’s a Big O Notation problem.

The Academic Barrier: Why Big O Matters 🎓

In class, Big O Notation is often explained as a series of math proofs that put students to sleep. You see O(n log n) or O(2^n) scribbled on a blackboard, and your eyes glaze over. But as someone who just completed the unit, I’m here to tell you that Big O Notation is simply a way of measuring how your code gets slower as your input gets larger. Think of it as a “Scale Grade.” If your code is Grade A, it means it remains speedy even as your input reaches a million records. But if your code is Grade F, it means it may work for your assignment but will kill your server if you’re a real-world developer.

Getting your loop wrong doesn’t just mean your code is slow; it means your code is a failure.

The Bottleneck: The O(N²) Nightmare 🐌

Students of HND level or higher often make the same mistake: a nested loop. Here is a simple example: finding duplicates in an array.

Let's think of a list of user IDs; we want to check if there are any IDs that are repeated. A new programmer might use a Brute Force approach. They take the first ID and compare it to all other IDs. They take the second ID and compare it to all other IDs.

In Java code:

// The O(N^2) "Brute Force" approach
public boolean hasDuplicate(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] == nums[j]) {
                return true; // Duplicate found
            }
        }
    }
    return false;
}

Enter fullscreen mode Exit fullscreen mode

What makes this a disaster?

It’s an O(n^2) algorithm. If you have an array of 10 items, it does 100 comparisons. If you have an array of 1,000 items, it does 1,000,000 comparisons. If you have an array of 100,000 records, your program does 10 billion operations. It maxes out the CPU and RAM.

The Solution: Make it O(N) Efficient ⚡

To solve this problem, stop comparing every item with every other item. Think of a more intelligent data structure. If you’re an object-oriented programmer, you can use a Hash Set or a Dictionary in Python. It’s a data structure that lets us remember what we have seen so far. We only need one loop.

The Python code looks like this:

# The O(N) Optimised approach
def has_duplicate(nums):
    seen = set() # Create a Hash Set
    for num in nums:
        if num in seen:
            return True # Duplicate found in "Constant Time"
        seen.add(num)
    return False

Enter fullscreen mode Exit fullscreen mode

The Transformation:

This runs in O(n), or linear time. What this means is that if you are working with 100,000 pieces of data, the computer is working through 100,000 operations. What used to take you minutes now takes less than a second to accomplish.

Real World Application: CRUD and SQL 🗄️

This is not just about loops. Loops are important in Database Development as well. Have you ever written a SQL query without an index? What happens is that you are performing an O(n) problem. What about if you are working with two unindexed tables? Suddenly you are working with an O(n × m) problem, or a problem equivalent to a nested loop. Remember to always ask yourself in your projects: “What happens if I add 10,000 more pieces of data to my application?”

Final Thoughts: Don’t Over-Engineer 🛠️

Efficiency is great, but don’t get caught up in micro-optimizations. If your list is only ever going to have 5 items, an O(n^2) loop is perfectly acceptable if it’s easier to read. In your Unit Graded Assessment, you need to demonstrate your understanding of this. Tell your lecturer that you picked a HashMap/HashSet because you had thought about Computational Complexity. This is what separates a “coder” from an “engineer.”

Top comments (0)