Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82876

question on string object handling in Python 2.7.8

From Dave Tian <dave.jing.tian@gmail.com>
Subject question on string object handling in Python 2.7.8
Date 2014-12-23 20:28 -0500
Newsgroups comp.lang.python
Message-ID <mailman.17176.1419410409.18130.python-list@python.org> (permalink)

Show all headers | View raw


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. 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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

question on string object handling in Python 2.7.8 Dave Tian <dave.jing.tian@gmail.com> - 2014-12-23 20:28 -0500
  Re: question on string object handling in Python 2.7.8 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-24 22:22 +1100
    Re: question on string object handling in Python 2.7.8 Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-24 09:39 -0700
  Re: question on string object handling in Python 2.7.8 Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-12-25 17:23 +1300
  Re: question on string object handling in Python 2.7.8 Denis McMahon <denismfmcmahon@gmail.com> - 2014-12-25 16:34 +0000

csiph-web