which is better, string concatentation or substitution?
Roy Smith
roy at panix.com
Mon May 8 09:28:40 EDT 2006
In article <445ec45e$0$1965$c3e8da3 at news.astraweb.com>,
John Salerno <johnjsal at NOSPAMgmail.com> wrote:
> My initial feeling is that concatenation might take longer than
> substitution, but that it is also easier to read:
>
>
> def p(self, paragraph):
> self.source += '<p>' + paragraph + '</p>\n\n'
>
> vs.
>
> def p(self, paragraph):
> self.source += '<p>%s</p>\n\n' % paragraph
>
>
>
> Is there a preference between these two ways?
One may be marginally faster, but they both require copying the source
string, and are thus both O(n). If you're just doing one or a small fixed
number of these, it really doesn't matter. Pick whichever one you think is
easier to read.
On the other hand, if you're doing a lot of them (i.e. in a loop), the
entire loop will now be O(n^2), which is a killer. If that's the case,
what you want to do is accumulate the individual substrings in a list, then
join the list elements all at once:
parts = []
for paragraph in foo:
parts.append ('<p>')
parts.append (paragraph)
parts.append ('<p>\n\n')
# or maybe instead of that ...
# parts += ['<p>', paragraph, '<p>\n\n']
self.source = "".join (parts)
This only requires a single copy, and thus you're back to being O(n), which
beats the heck out of O(n^2).
More information about the Python-list
mailing list