which is better, string concatentation or substitution?

Roy Smith roy at panix.com
Tue May 9 04:37:16 CEST 2006

In article <envv521lhcir4i9bpg2affcu2kg7gje41v at 4ax.com>,
 Casey Hawthorne <caseyhHAMMER_TIME at istar.ca> wrote:

> roy at panix.com (Roy Smith) wrote:
> >O(n^0), which is almost always written as O(1).  This is a "constant
> >time" algorithm, one which takes the same amount of steps to execute
> >no matter how big the input is.  For example, in python, you can
> >write, "x = 'foo'".  That assignment statement takes the same amount
> >of time no matter how long the string is.  All of these execute in the
> >same number of steps:
> >
> >	x = ''
> >	x = 'foo'
> >	x = 'a very long string with lots and lots of characters'
> >
> >We can say that "assignment is constant time", or "assignment is
> >O(1)".
> Constant time if converted to byte code or compiled?
> O(n) in string length if being interpreted?

Compiled, byte-code, or interpreted has nothing to do with it.

Assignment would be O(n) if it involved copying the string (as it might in 
C++ using std:string, for example), but in Python, assignnment simply 
involves binding a new name to an existing object.

More information about the Python-list mailing list