question on string object handling in Python 2.7.8

Ned Batchelder ned at nedbatchelder.com
Wed Dec 24 08:17:42 EST 2014


On 12/23/14 8:28 PM, Dave Tian wrote:
> Hi,
>
> There are 2 statements:
> A: a = ‘h’
> B: b = ‘hh’
>
> According to me understanding, A should be faster as characters would shortcut this 1-byte string ‘h’ without malloc; B should be slower than A as characters does not work for 2-byte string ‘hh’, which triggers the malloc.

I'm not sure why you thought a two-character string would require an 
extra malloc?  In Python 2.7, strings have a fixed-size head portion, 
then variable-length character storage.  These two parts are contiguous 
in one chunk of memory, for any length string.  Making a string requires 
one malloc, regardless of the size of the string.

What reference were you reading that implied otherwise?

> However, when I put A/B into a big loop and try to measure the performance using cProfile, B seems always faster than A.
> Testing code:
> for i in range(0, 100000000):
> 	a = ‘h’ #or b = ‘hh’
> Testing cmd: python -m cProfile test.py
>
> So what is wrong here? B has one more malloc than A but is faster than B?
>
> Thanks,
> Dave
>
>


-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list