Jump to content
  • Advertisement
Sign in to follow this  
blackviper91

[Python]Questions about time and sqare roots.

This topic is 3982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 time
from math import sqrt

starttime = 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).

Share this post


Link to post
Share on other sites
Thanks for the help. I finished the program. Here it is:

[source lang=python]#Computer SpeedTest
#By Cody
#10.28.07

import time
from math import sqrt

print "\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)*1000
totalSeconds = totalTime/1000
loopsPerSec = 50000000/totalTime
secPerLoop = totalTime/50000000
score = 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: ", score

raw_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]

Share this post


Link to post
Share on other sites
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 ***

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!