Pyhon 2.x or 3.x, which is faster?
alister
alister.ware at ntlworld.com
Sat Mar 12 10:36:38 EST 2016
On Sat, 12 Mar 2016 10:31:39 +0000, BartC wrote:
> On 12/03/2016 10:06, alister wrote:
>> On Fri, 11 Mar 2016 22:24:45 +0000, BartC wrote:
>>
>>> On 11/03/2016 21:59, Mark Lawrence wrote:
>>>> On 11/03/2016 18:57, BartC wrote:
>>>
>>>> def test():
>>>> s=""
>>>> for i in range(10000000):
>>>> s+="*"
>>>> print (len(s))
>>>>
>>>> test()
>>>
>>>> The minor snag that you might like to correct with your
>>>> microbenchmark,
>>>> which any experienced Python programmer knows, is that you *NEVER,
>>>> EVER*
>>>> create strings like this.
>>>
>>> Why not? Chris said his version runs much faster (even allowing for
>>> different machines), and might have a special optimisation for it.
>>>
>>> And I think it can be optimised if, for example, there are no other
>>> references to the string that s refers to.
>>>
>>> So what's wrong with trying to fix it rather that using a workaround?
>>
>> because the "workarround" is not a workarround it is the correct way to
>> do it the code above is a workarround for somone who does not know the
>> pythonic method to do this
>>
>> S= "*"*10000000
>
> This is a benchmark that measures the cost of adding to a string a
> character at a time.
>
> In practice the final length of the string is not known, and the
> characters added at each stage are not known.
>
> Although the strings won't usually be that big; the benchmark
> exaggerates here to highlight a possible issue with += on strings. And
> it worked, as there can be big difference with or without the +=
> optimisation in place.
>
> It also showed a possible bug or problem with PyPy.
>
> You can't demonstrate all this by just writing s="*"*10000000.
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.
if you wish to compare performance between one versio of python and
another at least try to start with pythonic code
--
There are two ways of disliking poetry; one way is to dislike it, the
other is to read Pope.
-- Oscar Wilde
More information about the Python-list
mailing list