PATCH: Speed up direct string concatenation by 20+%!

Larry Hastings larry at hastings.org
Fri Sep 29 12:09:04 EDT 2006


William Heymann wrote:
> This is a pretty small change but I would suggest xrange instead of range.

Good point!  Since I was calling range() during the benchmark, it was
timed too.  Switching to xrange() will mean less overhead.

I re-ran this benchmark (again):
  s = ""
  for i in range(100000):
    s += "a"

The result:
  Python 2.5 release: 30.1s
  Python 2.5 locally built: 30.4s
  Python 2.5 concat: 3.95s
  Improvement: *669%*!  (1/7.7 the time.)

So xrange(10000000) was adding between 0.4s and 0.9s overhead, and
losing it makes my approach look even better.  Thanks!


/larry/




More information about the Python-list mailing list