[Python]Questions about time and sqare roots.

Started by
4 comments, last by blackviper91 16 years, 5 months ago
Ok, first, everyone take a deep breath. I realize it may be a shock that there is someone who isn't using C++, but you can handle it :P But seriously, my question is fairly simple, what function in Python can I use to measure a short interval of time accurate to a millisecond (1/1000 of a second). Basically what I am doing is running a loop that calculates the squareroot of 91245 (there is no reason I picked that number, just random) 50 million times in a row and then measures how long that takes. I'll then use the data to generate speed score and stuff, I know this isn't necesarily an accurate benchmarks or anything, it is just kind of a fun little practice thing to see how fast my computer goes compared to the others in my house. Also, on a related note, what is the squareroot function in Python? I am thinking something like squrert or something, but I think it actually a leftover from another language stuck in my brain. Thanks for all the help, Cody
Advertisement
The time module can supply you with some basic timing methods. There is a squareroot function located inside the math module (named sqrt):

import timefrom math import sqrtstarttime = time.time()for x in xrange(50000000):    sqrt(91245)endtime = time.time()print starttime - endtime


I'd expect that sqrt's performance is pretty good - it's likely to be implemented in C. The real bottleneck will probably be generating your 50000000-long list (which xrange will help alleviate).
Thanks for the help. I finished the program. Here it is:

[source lang=python]#Computer SpeedTest#By Cody#10.28.07import timefrom math import sqrtprint "\t\t\tComputer SpeedTest\n"raw_input("Press enter to start the test, this may cause other programs to TEMPORARILY lock up.")print "Please wait, this may take several minutes. Testing... ",startTime = time.time()for x in xrange(50000000):    sqrt(91245)endTime = time.time()print "Complete"totalTime = (endTime - startTime)*1000totalSeconds = totalTime/1000loopsPerSec = 50000000/totalTimesecPerLoop = totalTime/50000000score = 5000 - (secPerLoop * 1000000)print "\nIt took ", totalTime, " milliseconds (", totalSeconds, " seconds) for the computer to complete 50 million loops."print "That is ", loopsPerSec, " loops every millisecond."print "Your score is: ", scoreraw_input("Press enter to exit...")


I got a 4350 BTw.

Thanks again,
Cody

EDIT: Added source tags and changed the code just a bit.

[Edited by - blackviper91 on October 29, 2007 5:31:32 PM]
Quote:Original post by blackviper91
(How do I put this in code tags like you did? I couldn't make anything work)

Quote:from How do I put links/quotes/images/code/smileys in my posts? in the FAQ
When posting source code, you may want to use the [source][/source] tag, which will produce a formated, color coded, easy-to-read table. If you only want to preserve spacing, but not use the other features of the source tag, try using the tag instead.

You also have the option of utilizing the source tag to highlight different programming languages. If you do not specify the language, C++ will be assumed. The following languages are available:
*** List removed - follow the link if you want to find out about this ***

- Jason Astle-Adams

The timeit module is designed for tasks such as this one.
Thanks for the links guys. I added source tags to my post and I am looking into the timeit module.

~Cody

This topic is closed to new replies.

Advertisement