1

给定大量数字,其中:

[1, 2, 3, 4 ...] => <tag attrib="1" />
                    <tag attrib="2" />
                    <tag attrib="3" />
                    <tag attrib="4" />
                    ...

哪个更有效/更快:

a)使用它们从头开始构建它们并将它们Element("name", attributes)附加到某个根

或者

b)fromstring(str)其中 str 是示例中这些标签的字符串表示形式

4

1 回答 1

0

更新测试:

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 是最快的版本。所以我会说:不要使用字符串可憎!;-)

于 2012-07-11T23:59:54.753 回答