Hi,
I have updated my example, so that it exactly matches the website. Now it
is Ok. I don't see what was wrong in my first example.

Regards
Sébastien



> Hi,
> Thank you for your replies.
>
> I fully understand your point, Steve, and agree with it, but I just wanted
> to check if one should really "Avoid dots" as suggested on the Python
> website itself. I wonder if this section should not be removed, since the
> given example itself is wrong (I mean slower).
> I will post this message on the mailing list,
>
> Regards
>
> 2016-12-02 13:14 GMT+01:00 Steve Holden <st...@holdenweb.com>:
>
>> On Fri, Dec 2, 2016 at 11:31 AM, Chris Angelico <ros...@gmail.com> wrote:
>>
>>> Best would be to start a discussion on the main python-l...@python.org
>>> mailing list. There is a LOT to discover about optimization and
>>> timing, and very few of us are experts, but most of us know how very
>>> inexpert we are :)
>>>
>>
>> Also, remember that premature optimization is the root of all evil in
>> programming! The kind of gains to be expected by hoisting attribute lookups
>> out of the loop will usually be lost in the noise compared with gains to be
>> made from use of superior algorithms (often involving refactoring
>> inappropriate data structures).
>>
>> In the particular code you submitted I saw that the "optimized" version
>> is about 10% slower than the "non-optimzed" version. Until you get your
>> algorithm correct, it's hardly worth worrying about. After all, the
>> Pythonic way to create the value you want is simply
>>
>>     "WORD" * 100000
>>
>> Here's the output from an updated program where the third function simply
>> computes that expression:
>>
>> python3 /tmp/times.py
>> 2.358054072014056
>> 2.167076243989868
>> 0.002145289006875828
>>
>> It seems to me that proves the point exactly: focus on efficient
>> algorithms!
>>
>> I understand you may simply be exploring python's behaviour, which is
>> great, but as far as production work goes, the old saw is true:
>>
>> "First, make your program work; then, if it doesn't work fast enough,
>> make it work faster"
>>
>> regards
>>  Steve
>>
>> Steve Holden
>>
>
>
#!/usr/bin/env python
# coding: utf-8

import timeit

oldlist = ["toto"] * 100000

def f():
    newlist = []
    for word in oldlist:
        newlist.append(word.upper())
    
def g():
    upper = str.upper
    newlist = []
    append = newlist.append
    for word in oldlist:
        append(upper(word))

print(timeit.timeit('f()', setup="from __main__ import oldlist, f", number=100))
print(timeit.timeit('g()', setup="from __main__ import oldlist, g", number=100))

_______________________________________________
pydotorg-www mailing list
pydotorg-www@python.org
https://mail.python.org/mailman/listinfo/pydotorg-www

Reply via email to