Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of string concatenation in Python [duplicate]

Tags:

I'm analysing the complexity of my code. From what I found online, since strings are immutable in python, a concatenation of a string and a character should be O(len(string) + 1).

Now, here is my piece of code (simplified):

word = "" for i in range(m):     word = char_value + word return word 

The total time complexity should be:

(0+1) + (1+1) +...+ m = m(m+1)/2 = O(m^2)

Is this correct?

like image 738
cwbrd Avatar asked May 10 '16 08:05

cwbrd


People also ask

What is the time complexity of string concatenation Python?

The time complexity of using join() for strings is O(n) where n is the length of the string to be concatenated.

What is the time complexity of string concatenation?

On each concatenation a new copy of the string is created, so that the overall complexity is O(n^2). In Java, the complexity of s1. concat(s2) or s1 + s2 is O(M1 + M2) where M1 and M2 are the respective String lengths.

What is the most efficient way to concatenate many strings together Python?

The best way of appending a string to a string variable is to use + or +=. This is because it's readable and fast. They are also just as fast.

Is Join faster than concatenation Python?

String join is significantly faster then concatenation.


1 Answers

Yes, in your case*1 string concatenation requires all characters to be copied, this is a O(N+M) operation (where N and M are the sizes of the input strings). M appends of the same word will trend to O(M^2) time therefor.

You can avoid this quadratic behaviour by using str.join():

word = ''.join(list_of_words) 

which only takes O(N) (where N is the total length of the output). Or, if you are repeating a single character, you can use:

word = m * char 

You are prepending characters, but building a list first, then reversing it (or using a collections.deque() object to get O(1) prepending behaviour) would still be O(n) complexity, easily beating your O(N^2) choice here.


*1 As of Python 2.4, the CPython implementation avoids creating a new string object when using strA += strB or strA = strA + strB, but this optimisation is both fragile and not portable. Since you use strA = strB + strA (prepending) the optimisation doesn't apply.

like image 114
Martijn Pieters Avatar answered Sep 19 '22 20:09

Martijn Pieters