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²)
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