FAQ
Hi all,

I'm facing a strange issue in Python execution speed. I'm running the
following test script:

---------------------------------8<-----------------------------------------

dim = 1000
iteration = 100000

list1 = []
list2 = []

for i in range(dim):
list1.append(float(i))
list2.append(float(i) * 0.264)

for k in range(iteration):
for j in range(dim):
ris = 0
ris = ris + list1[j] + list2[j]

---------------------------------8<-----------------------------------------

which runs in about 80 seconds on my local hardware (mean of multiple
execution)
If I move the whole code into a function and call this latter the execution
time drops to about 45 seconds.

What is the reason of this improvement?

Thank you very much in advance.

Leonardo Giordani
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100412/8ad88a7d/attachment.html>

Search Discussions

  • Chris Rebert at Apr 12, 2010 at 10:24 am

    On Mon, Apr 12, 2010 at 2:51 AM, Leonardo Giordani wrote:
    <top-level module code snipped>
    which runs in about 80 seconds on my local hardware (mean of multiple
    execution)
    If I move the whole code into a function and call this latter the execution
    time drops to about 45 seconds.

    What is the reason of this improvement?
    The top-level module namespace (i.e. global scope) is implemented
    using a dictionary (i.e. hash table).
    Names in local function scope instead use precalculated offsets into an array.
    The latter is faster than the former, hence the speed difference.

    Cheers,
    Chris
  • Stefan Behnel at Apr 12, 2010 at 10:30 am

    Leonardo Giordani, 12.04.2010 11:51:
    I'm facing a strange issue in Python execution speed. I'm running the
    following test script:

    ---------------------------------8<-----------------------------------------

    dim = 1000
    iteration = 100000

    list1 = []
    list2 = []

    for i in range(dim):
    list1.append(float(i))
    list2.append(float(i) * 0.264)
    What about this:

    list1 = list(map(float, xrange(dim)))
    list2 = [f * 0.264 for f in list2]

    for k in range(iteration):
    for j in range(dim):
    ris = 0
    ris = ris + list1[j] + list2[j]
    What result do you expect for 'ris' after the last two lines?

    ---------------------------------8<-----------------------------------------

    which runs in about 80 seconds on my local hardware (mean of multiple
    execution)
    If I move the whole code into a function and call this latter the execution
    time drops to about 45 seconds.

    What is the reason of this improvement?
    Local variables in a function can be accessed much faster than globally
    defined names (which require a dictionary lookup). This is a good thing
    because the access to local variables tends to be much more performance
    critical than that to globals.

    Stefan
  • Leonardo Giordani at Apr 12, 2010 at 10:42 am
    Thank you Chris and Stefan, this was the answer I was looking for.

    Leonardo Giordani
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100412/b7150187/attachment.html>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 12, '10 at 9:51a
activeApr 12, '10 at 10:42a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase