Algorithms: Consequences of adding more loops

Az Codez
2 min readJan 8, 2023
Photo by Anna Dziubinska on Unsplash

Code Example

Let's consider this code

As you see here, there is a loop inside a loop.

Time Complexity

If you write a loop inside a loop, the run time of this algorithm increases dramatically, and in turn, your server will run harder.

The run time of an algorithm is specified by O(n)

O(n) = one loop

O(n²) = two loops

As you see below, the exponential curve of how long the algorithm runs in O(n²)

Chart from https://www.bigocheatsheet.com/

It takes more time for operations to run since operations increase sharply from when the algorithm started.

Consequences

  • Slower running times for your algorithm
  • Your app can fall over with no response to your site
  • Loose clients
  • Loose money

Conclusion

--

--

Az Codez
Az Codez

Written by Az Codez

Cloud Software Engineer focusing on PHP, Python, JS, CI/CD, GitOps, AWS, Azure, Adobe Commerce. I enjoy code for good projects, tech, gym, nature & travel

Responses (1)