Message165508
Thank you, Eli.
However changes to tostring() and tostringlist() break the invariant b"".join(tostringlist(element, 'utf-16')) == tostring(element, 'utf-16'). You should add followed methods to DataStream:
def seekable(self):
return True
def tell(self):
return len(data)
Note, that monkey-patched version is faster.
stream = io.BufferedIOBase()
stream.writable = lambda: True
stream.write = data.append
stream.seekable = lambda: True
stream.tell = data.__len__
Benchmark results:
tostring() with BytesIO:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')" "ET.tostring(e, 'utf-16')"
1000 loops, best of 3: 268 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )" "ET.tostring(e, 'utf-16')"
100 loops, best of 3: 4.63 msec per loop
tostring() with monkey-patching:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')" "ET.tostring(e, 'utf-16')"
1000 loops, best of 3: 263 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )" "ET.tostring(e, 'utf-16')"
100 loops, best of 3: 3.84 msec per loop
tostringlist() with DataStream class:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')" "ET.tostringlist(e, 'utf-16')"
1000 loops, best of 3: 624 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )" "ET.tostringlist(e, 'utf-16')"
100 loops, best of 3: 4.09 msec per loop
tostringlist() with monkey-patching:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')" "ET.tostringlist(e, 'utf-16')"1000 loops, best of 3: 259 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )" "ET.tostringlist(e, 'utf-16')"
100 loops, best of 3: 3.81 msec per loop |
|
Date |
User |
Action |
Args |
2012-07-15 07:02:39 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, effbot, nnorwitz, amaury.forgeotdarc, bugok, rurwin, eli.bendersky, flox, BreamoreBoy, Richard.Urwin, python-dev |
2012-07-15 07:02:39 | serhiy.storchaka | set | messageid: <1342335759.61.0.225517200005.issue1767933@psf.upfronthosting.co.za> |
2012-07-15 07:02:39 | serhiy.storchaka | link | issue1767933 messages |
2012-07-15 07:02:38 | serhiy.storchaka | create | |
|