Here is how to do short timing tests using the timeit module from the command line on windows. (C> is the prompt. C> python -m timeit -s "import random" random.randint(1, 100) or C> python -m timeit -s "import random" int(1 + random.random()*99) The above may be easier for short code fragments like the ones you were looking at. Richard ----- Original Message ----- From: "Jacob Kruger" <jacob@xxxxxxxxxxxxx> To: <program-l@xxxxxxxxxxxxx> Sent: Monday, October 01, 2012 1:26 PM Subject: [program-l] Re: Python list access speeds Ok, you were right - just did something similar again, and difference was: It took 0.167999982834 seconds to do randInt 100000 times It took 0.0449998378754 seconds to do random and convert output 100000 times Here's the code: #start code import random, time tInt = time.time() for I in range(1, 100000): iRand = random.randint(1, 100) print "It took " + str(time.time() - tInt) + " seconds to do randInt 100000 times" tRand = time.time() for I in range(1, 100000): fRand = random.random() iRand = int(fRand * 100) print "It took " + str(time.time() - tRand) + " seconds to do random and convert output 100000 times" #end code Jacob Kruger Blind Biker Skype: BlindZA '...fate had broken his body, but not his spirit...' ----- Original Message ----- From: "R Dinger" <rrdinger@xxxxxxxxxx> To: <program-l@xxxxxxxxxxxxx> Sent: Monday, October 01, 2012 4:42 PM Subject: [program-l] Re: Python list access speeds > Regarding your random tests: reducing time from 7 seconds to 2.7 is still > 2.59 times faster and that could be significant. > > I vaguely recall your test, but I thought it was much closer. > > Richard > ----- Original Message ----- > From: "Jacob Kruger" <jacob@xxxxxxxxxxxxx> > To: <program-l@xxxxxxxxxxxxx> > Sent: Sunday, September 30, 2012 9:36 PM > Subject: [program-l] Re: Python list access speeds > > > 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 > > ** 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 ** 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