better way than: myPage += 'more html' , ...

François Pinard pinard at iro.umontreal.ca
Thu Jun 26 09:09:51 EDT 2003


[Max M]

> # renaming the append method makes it a bit easier on the eye.

> htmlPage=[]
> a = htmlPage.append # ah dynamic languages
> a("<html><header></header><body>")
[...]
> htmlDoc="\n".join(htmlPage)

> # using cStringIO should be the fastest approach

> import cStringIO
> io = cStringIO()
> a = io.write
> a("<html><header></header><body>\n")
[...]
> htmlDoc = io.getvalue()
> io.close()

Your usage of "should" is Interesting :-).  I wonder what it really means;
either that you did not time it, or that you timed it and found it not to be
the fastest.  I'm a big fan of the `.append'-based method, but would switch
to `cStringIO' if it was significantly faster[1].  I guess that both methods
are roughly O(N), but would suspect the `cStringIO' might have a higher
start factor, making it less appealing for only a few short strings.  Did
someone actually timed both methods compared, and got an opinion?

--------------------
[1] And also given, for stylistic reasons, that the `io.close()' can be
spared.  The first example does not explicitely empty `htmlPage'.  But this
is likely an orthogonal issue and another debate.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list