What is wrong the piece of

Indeed, at the first iteration, we already know how many times we need to iterate; when it's time for the second iteration, a recalculation of the already known value is done once again ... we keep doing this n times.

Hence, complexity here is unnecessarily high.

Detailing the code above in a more explicit manner, it may be translated as follows:

The easy optimisation is to add a variable

Besides, such enhancements are irrelevant when it comes to small data.

__C Language__code below?There is a recalculation of the length of the string at every iteration, which is a major waste of time.for( i = 1 ; i <= length(str) ; i ++ ) { // Do some stuff }

Indeed, at the first iteration, we already know how many times we need to iterate; when it's time for the second iteration, a recalculation of the already known value is done once again ... we keep doing this n times.

Hence, complexity here is unnecessarily high.

Detailing the code above in a more explicit manner, it may be translated as follows:

At a glance, not only the code becomes redundant and longer, this fragment's time complexity is obviouslyintn = 0;while(str[i] != '\0') { n ++; }for( i = 1 ; i <= n ; i ++ ) { // Do some stuff n = 0;while(str[i] != '\0') { n ++; } }

**O(n²)**.The easy optimisation is to add a variable

**n**, which will hold the length of the string, and, consequently, no additional computations would be needed anymore:intn = length(str);

This easy fix allows to pass from a quadratic time complexity to a linear one -for( i = 1 ; i <= n ; i ++ ) { // Do some stuff }

**O(n)**.Besides, such enhancements are irrelevant when it comes to small data.