[pypy-dev] performance issue with context managers

Maciej Fijalkowski fijall at gmail.com
Thu May 16 11:45:20 CEST 2013

On Thu, May 16, 2013 at 9:14 AM, Jasper Spaans <spaans at fox-it.com> wrote:
> Hi list,
> I was toying around a bit with writing a statistical profiler in python,
> and came up with https://gist.github.com/jap/5584946
> For reference, the main routine is:
> with Profiler() as p:
>     with ProfilerContext("c1"):
>         s = ""
>         for t in range(100000):
>             with ProfilerContext("c2"):
>                 s = s + "a"
>             s = s + "b"
>         print p.get_data()

Also, it's not that string concatenation has poor performance, it has
quadratic performance. It's the same as in cpython if you made two
references to s, your performance will plummet. (each s + 'a' would do
a copy). We simply don't have the refcount hack

Please use l.append('a') and later ''.join()

More information about the pypy-dev mailing list