Time Complexity of String Operations

Y

youtoo

It has been extensively discussed the time complexity (quadratic) of
string concatenation (due to string's immutability).
But what is:

== the time complexity of string indexing? Is it constant?
== the time complexity of string slicing? Is it O(K) with K the
slice's length?

How are strings stored in Python? As arrays? As linked lists?

Thanks a lot!

yt.
 
D

David Wahler

It has been extensively discussed the time complexity (quadratic) of
string concatenation (due to string's immutability).

Actually, it is roughly linear, at least for reasonable string lengths:

$ python -V
Python 2.5.2
$ python -mtimeit -s "n=1000; a='#'*n" "a+a"
1000000 loops, best of 3: 1 usec per loop
$ python -mtimeit -s "n=10000; a='#'*n" "a+a"
100000 loops, best of 3: 5.88 usec per loop
$ python -mtimeit -s "n=100000; a='#'*n" "a+a"
10000 loops, best of 3: 59.8 usec per loop

Repeatedly constructing a string by appending a constant number of
characters at a time, however, is quadratic in the final string length
(although VM optimizations may affect this).
But what is:

== the time complexity of string indexing? Is it constant?
Yes.

== the time complexity of string slicing? Is it O(K) with K the
slice's length?

I suspect so, since the time is dominated by the time taken to copy
the data into a new string object.
How are strings stored in Python? As arrays? As linked lists?

Arrays; see Include/stringobject.h in the Python source distribution.

-- David
 
T

Terry Reedy

youtoo said:
It has been extensively discussed the time complexity (quadratic) of
string concatenation (due to string's immutability).
But what is:

== the time complexity of string indexing? Is it constant?
== the time complexity of string slicing? Is it O(K) with K the
slice's length?

How are strings stored in Python? As arrays? As linked lists?

There is a Py wiki page on such issues. A wiki search should find it.
 
Y

youtoo

Actually, it is roughly linear, at least for reasonable string lengths:

$ python -V
Python 2.5.2
$ python -mtimeit -s "n=1000; a='#'*n" "a+a"
1000000 loops, best of 3: 1 usec per loop
$ python -mtimeit -s "n=10000; a='#'*n" "a+a"
100000 loops, best of 3: 5.88 usec per loop
$ python -mtimeit -s "n=100000; a='#'*n" "a+a"
10000 loops, best of 3: 59.8 usec per loop

Repeatedly constructing a string by appending a constant number of
characters at a time, however, is quadratic in the final string length
(although VM optimizations may affect this).



I suspect so, since the time is dominated by the time taken to copy
the data into a new string object.


Arrays; see Include/stringobject.h in the Python source distribution.

-- David

Thank you very much!

yt.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top