List to string? Docstrings?

Tim Peters tim_one at email.msn.com
Mon Sep 13 22:55:39 EDT 1999


[François Pinard, on
       text = reduce(operator.concat, chars)
 vs
       text = string.join(chars, '')
]
> ...
> Thinking more about it, it seems quite reasonable to guess that the reduce
> method shows a quadratic time behaviour in the list size, because of all
> intermediary results,

Yes.

> while string.join _may_ be implemented to use linear time in the list
size.

On most systems most of the time, string.join is linear-time in the length
of the result string.  See strop_joinfields in Modules/stropmodule.c for all
the gore.

Another possibility is to create a character array (array.array('c')), fill
it with the chars via .fromlist(), then get the string out via .tostring().
string.join will almost always be faster, but using the character array
method has better worst-case behavior (always linear-time).  But if you're
bursting your strings into characters to begin with because what you really
wanted was a mutable string, it may be faster overall to do everything with
a character array from the start instead.  Or, maybe not.

if-you-want-easy-answers-use-fortran<wink>-ly y'rs  - tim






More information about the Python-list mailing list