更新されたテスト:
from xml.etree.ElementTree import Element, XML, tostring
from timeit import timeit
elist = [e for e in xrange(1000)]
def test_normal():
eroot = Element('root')
for e in elist:
eroot.append(Element("tag", {"attrib" :"%s" %e}))
def test_list():
eroot = Element('root')
[eroot.append(Element("tag", {"attrib" :"%s" %e})) for e in elist]
print "etree: %.6f" %timeit(test_normal, number=1000)
print "l-cmp: %.6f" %timeit(test_list, number=1000)
from xml.etree.cElementTree import Element, XML, tostring, fromstring
#from lxml.etree import Element, XML, tostring
print "ctree: %.6f" %timeit(test_normal, number=1000)
print "c-cmp: %.6f" %timeit(test_list, number=1000)
def test_string():
eroot = "<root>"
tags = ['<tag attrib="%s" />' %e for e in elist]
eroot += ' '.join(tags) + '</root>'
tree = fromstring(eroot)
print "strng: %.6f" %timeit(test_string, number=1000)
etree: 13.302093
l-cmp: 12.276725
ctree: 5.482961
c-cmp: 5.692758
strng: 6.578780
cElementTree は最速のバージョンです。だから私は言うだろう:文字列の嫌悪感を使わないでください!;-)