5 Replies - 501 Views - Last Post: 22 November 2012 - 01:27 PM Rate Topic: -----

#1 ricardo49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 12

Timing the execution of a Python program

Posted 22 November 2012 - 07:09 AM

I'm fairly sure that in Python there is some way of making a call to my computer clock before and after the execution of a program in order to determine how many minutes and seconds have elapsed. I would like to add such code to the following program to do that. It doesn't have to be anything fancy, like down to the millisecond -- in this case, minutes and seconds would be sufficient. The program would be run using IDLE, which is the only way I know how to run Python programs at this point. Any help or suggestions would be appreciated.

# pgm35: prints all primes between 0 and 10 million preceded by the sequence
# number of each

print("1: 2")
print("2: 3")
print("3: 5")
print("4: 7")
counter=4
import math
for x in range (9,9999999,2):
    root=int(math.sqrt(x)) # make sure that 'root' is an integer
    if root%2==0: # make sure that root is odd
        root=root+1
    else:
        root=root+2
    d=3 # starting point for division by odd numbers
    while x%d!=0:
        d=d+2
        if d>root:
            counter=counter+1
            print(str(counter)+": "+str(x))
            break


Is This A Good Question/Topic? 0
  • +

Replies To: Timing the execution of a Python program

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Timing the execution of a Python program

Posted 22 November 2012 - 07:14 AM

If you look there is a tutorial or a snippet on this site on how to achieve it.
Was This Post Helpful? 0
  • +
  • -

#3 ricardo49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 12

Re: Timing the execution of a Python program

Posted 22 November 2012 - 11:17 AM

Your suggestion was helpful in one sense but not in another. I'm aware of the 'time' module because it's mentioned on page 516 of one of my Python textbooks. I'm fairly new to Python and to programming PC's in general. I'm taking an online tutorial provided by the University of Waterloo (Ontario), where I'm on chapter 12 of 18 chapters. The next chapter is on lists, which I'm excited about because I can use a list to make the above program execute somewhat faster. Although the tutorial has covered the defining of a function, my understanding of Python is not advanced enough to put to good use the previously mentioned "snippet" about timing a function. I do hope to get there one day in the not-too-distant-future. In the meantime, Id like to put what Python I have learned to some use. That's one reason I wrote the program and why I plan to upgrade it once I learn about lists. I could time the execution of the program with a watch and get an accurate enough estimate to satisfy my curiosity; but I was hoping a few simple lines added to the beginning and end of the program would help me time not only this program but similar ones I may write in the near future...as well as illustrate how the 'time' module can be put to use.
Was This Post Helpful? 0
  • +
  • -

#4 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Timing the execution of a Python program

Posted 22 November 2012 - 12:49 PM

If I were you, I'd read the entire thread, but at least take a look at baavgai's post.
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Timing the execution of a Python program

Posted 22 November 2012 - 01:12 PM

While I did write a simple snippet for timing a function, it's probably better to use the timeit module.
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing the execution of a Python program

Posted 22 November 2012 - 01:27 PM

Nallo wrote a good tutorial on timeit, in response to your first one.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1