It's the number of iterations that differ.
In the second snippet you have n
iterations. But in the first snippet you have roughly log n
iterations. Which means the complexity of the function goes from O(n)
down to O(log n)
which for huge n
might matter.
How does it work? Here as a starter the logic summarized in an oversimplified way: If we half our iterations, we can double what we add to the result.
So >> 1
works kind of like integer division by 2
. Integer divisions means for 5 / 2
we get 2
as a result. But so that means we are missing out on iterations!?
On every odd number we add the current string before doubling it to the result, so we kinda save that iteration's sum (the current str) in our result before continuing. So knowing our iterations are save we iterate down by kind off halving our remaining iterations each step while doubling what we soon add to the result.
By this duplication we skip a lot of iterations, which is the key to the lowered complexity.
For n = 5
:
1 'str: ' 'kk' 'res: ' 'k'
2 'str: ' 'kkkk' 'res: ' 'k'
3 'str: ' 'kkkk' 'res: ' 'kkkkk'
For n = 10
:
1 'str: ' 'kk' 'res: ' ''
2 'str: ' 'kkkk' 'res: ' 'kk'
3 'str: ' 'kkkkkkkk' 'res: ' 'kk'
4 'str: ' 'kkkkkkkk' 'res: ' 'kkkkkkkkkk'
For n = 11
1 'str: ' 'kk' 'res: ' 'k'
2 'str: ' 'kkkk' 'res: ' 'kkk'
3 'str: ' 'kkkkkkkk' 'res: ' 'kkk'
4 'str: ' 'kkkkkkkk' 'res: ' 'kkkkkkkkkkk'