Pyhon 2.x or 3.x, which is faster?

Steven D'Aprano steve at pearwood.info
Sat Mar 12 22:22:43 EST 2016


On Sun, 13 Mar 2016 02:36 am, alister wrote about building up strings by
repeated concatenation:


> So you are bench marking python performance on a programming paradigm
> that is not good python practice.
> 
> A pointless exercise
> what may be the best way to achieve a rsult in one language is not
> necessarily the best way to achieve it in another.

"Pointless"? I don't think so.

The whole *point* is see how a straightforward and simple idiom compares
between one implementation (or language) and another. There's nothing wrong
with that, unless you think that the only code you should benchmark is code
you think will be fast. If your aim is to *hide the fact* that vanilla
Python performs poorly on repeated concatenation, then by all means this
benchmark is a bad benchmark.

You might also decide that it's a "bad" benchmark if you simply don't care
about the results. But why would you do that? Repeated string concatenation
is not only legal python code, but CPython actually includes special code
to optimize that case. Using CPython 2.7 on Linux, contrast the time taken
bythe optimized concatenation example:

py> with Stopwatch():
...     s = ''
...     for i in range(10**6):
...             s = s + '%'
...
time taken: 0.361933 seconds



with an unoptimized variation in all it's quadratic horror:

py> with Stopwatch():
...     s = ''
...     for i in range(10**6):
...             s = '%' + s
...
time taken: 1075.427070 seconds



(Source for Stopwatch available on request.)

Somebody took the time and effort to optimize the first case, and you think
it is "pointless" to see if it actually works? I disagree.

What we conclude from the results of the benchmark is a separate issue from
the results themselves. We might legitimate conclude "well don't write your
code like that". We might conclude "this is a problem that needs fixing".
We might punt, as indeed the Python core developers have done for this
specific one, and decided that the language Python need not make any
performance guarantees about repeated string concatenation, but
implementations are allowed to optimize it or not, as they prefer. The
speed of repeated string concatenation is a *quality of implementation*
issue.

Exactly the sort of thing which, arguable, ought to be benchmarked.



-- 
Steven




More information about the Python-list mailing list