[pypy-issue] [issue1356] Casting a str into a ctype pointer fails

Vincent Pelletier tracker at bugs.pypy.org
Mon Jan 7 21:25:31 CET 2013

Vincent Pelletier <plr.vincent at gmail.com> added the comment:

I special-cased for PyPy. I needed the special case for big-endian anyway, so I reused that code path. Result (total script, not 
just this part) is 5~6 times faster with PyPy than CPython with the hack, so I can do without the 10%-ish the hack gains on 
CPython. STand-alone CPython benchmark:

$ python -mtimeit -s "from struct import unpack; from cStringIO import StringIO; data = '\x23\x01' * 1000" "read = 
StringIO(data).read" "for _ in xrange(len(data) / 2): unpack('<H', read(2))"
1000 loops, best of 3: 255 usec per loop
$ python -mtimeit -s "from ctypes import cast, c_ushort, POINTER; c_ushort_p = POINTER(c_ushort); from cStringIO import StringIO; 
data = '\x23\x01' * 1000" "read = StringIO(data).read" "cast(data, c_ushort_p)"
1000000 loops, best of 3: 1.34 usec per loop

StringIO() call preserved in the latter to offset both result equally - alone it costs 0.136 usec per loop here.

I guess this report can be closed "wontfix".

Side note: I noticed a logical inversion in my original report: the byte swap must be done when on big-endian machines, not on 
little-endian. IE, I read little-endian short ints from file.

PyPy bug tracker <tracker at bugs.pypy.org>

More information about the pypy-issue mailing list