timing solutions for Euler

This is the place for queries that don't fit in any of the other categories.

timing solutions for Euler

Postby mnutz » Tue Nov 26, 2013 5:58 am

Hey all,

I've picked up Python again, built my second program and instead of just leaving it and forgetting it all like I do all the time I've decided to jump in to as much as possible and taking on Project Euler too.

Silly question I am sure, but how would you go about timing how long it takes to complete a solution?

I've seen it somewhere in the last few days but can't find it anywhere now :(
mnutz
 
Posts: 10
Joined: Sun Nov 17, 2013 8:11 am

Re: timing solutions for Euler

Postby micseydel » Tue Nov 26, 2013 7:22 am

If you're using Linux or OS X you can type "time" before your Python invocation. Otherwise you can use timeit.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: timing solutions for Euler

Postby zeycus » Tue Nov 26, 2013 4:01 pm

There is a module called 'timeit' that is precisely built for that:
http://docs.python.org/2/library/timeit.html
I guess this is really the way to go.

If the times you are measuring are not too short and you don't need a lot of precision, you can also write

Code: Select all
t_Init=time.time()
... execute your code
t_End=time.time()
print(t_End-t_Init)


You will get the number of seconds elapsed.
Image

Live long and prosper.
Spock
User avatar
zeycus
 
Posts: 23
Joined: Sun Feb 17, 2013 10:30 am
Location: Madrid

Re: timing solutions for Euler

Postby mnutz » Wed Nov 27, 2013 3:38 pm

thank you both for the info :D
mnutz
 
Posts: 10
Joined: Sun Nov 17, 2013 8:11 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests