Was really just testing the two versions of generating a random number between 1 and 100, as a purely test process, and, don't normally bother with things like this too much - so didn't need to be perfectly efficient/effective - but will have a look at timeit anyway.
If do want something to run in background - not really relevant in this case either - I would invoke a threading.Thread, and let it loop based on the state of something like a global boolean variable/value, and generally, also let it make use of time.sleep() in it's loop to pause the thread's execution inbetween, etc., but, that's more to do with real time state changing/monitoring, as opposed to interface interaction.
Stay well Jacob Kruger Blind Biker Skype: BlindZA '...fate had broken his body, but not his spirit...'----- Original Message ----- From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx> Sent: Tuesday, October 02, 2012 1:02 AM Subject: [program-l] Re: Python list access speeds
FWIW, you should be using timeit irathr than time. Also wrap your test case in a function and use timeit with number=100 or something multiple times to get the best and worse case. You'll want the average of those, since times can varry from test to test.On 10/1/2012 2:26 PM, Jacob Kruger wrote:Ok, you were right - just did something similar again, and difference was:It took 0.167999982834 seconds to do randInt 100000 timesIt took 0.0449998378754 seconds to do random and convert output 100000 timesHere'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 speedsRegarding your random tests: reducing time from 7 seconds to 2.7 is still2.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 eitherrandom.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 differencewas 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 speedsI 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 timingtests. 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 milliontimes. 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 secSo access by subscripting from a list generated by the range function istheslowest. 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 muchfaster. Finally in the last test I used the enumerate object to generate the subscripts in case that was needed by the application in addition tothe list value.The timeit module is quite handy, but be careful it can be quite addictiveand 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-- Take care, Ty http://tds-solutions.net The aspen project: a barebones light-weight mud engine: http://code.google.com/p/aspenmudHe that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.** 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