[program-l] Re: Python list access speeds

  • From: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
  • To: <program-l@xxxxxxxxxxxxx>
  • Date: Mon, 1 Oct 2012 06:36:12 +0200

Reminds me of a test I did - put together two loops where used either random.random() and converted the result to a number ranging from 1 to 100 sort of manually, and then did the same loop, but using random.randint(1, 100), and got them to both operate like 100000 times, and it was actually slightly quicker to just use random.random(), but anyway - think difference was like 2.7 seconds, compared to like 7 seconds.


Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

----- Original Message ----- From: "R Dinger" <rrdinger@xxxxxxxxxx>
To: "programming" <program-l@xxxxxxxxxxxxx>
Sent: Monday, October 01, 2012 1:01 AM
Subject: [program-l] Python list access speeds


I got to thinking about list access speeds when reading replies to Al's
iterator reset question last week, so I decided to do some testing with the
timeit module.  I tested access by subscript and by direct iteration.

I used the following setup code (a list of 500 random numbers) for all the
tests.  The time to execute the setup code is not included in the timing
tests.
setup = """
from random import random
valList = []
for i in xrange(500):
 valList.append(random())
"""

The following code fragments were tested each running the loop 1 million
times.
stmt1 = """
for i in range(len(valList)):
 v = valList[i]
"""
time: 8.01 sec

stmt2 = """
for i in xrange(len(valList)):
 v = valList[i]
"""
time: 7.43 sec

stmt3 = """
for v in valList:
 pass
"""
time: 2.67 sec

stmt4 = """
for i, v in enumerate(valList):
 pass
"""
time: 6.12 sec

So access by subscripting from a list generated by the range function is the slowest. In the second test using xrange (a generator) helped a little, but not much. The third test using direct iteration of the list is clearly much
faster.  Finally in the last test I used the enumerate object to generate
the subscripts in case that was needed by the application in addition to the
list value.

The timeit module is quite handy, but be careful it can be quite addictive
and lead to late nights testing all sorts of code fragments.

Richard

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: