time factor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deatheater1900
    New Member
    • Mar 2007
    • 2

    time factor

    how do you put time as a factor in python as i'm trying to make a loop for a given time.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by deatheater1900
    how do you put time as a factor in python as i'm trying to make a loop for a given time.
    The time module as has time function that returns a floating point which is the number of seconds since January 1st 1970. The easiest thing is to:
    Code:
    from time import time, sleep
    now = time()
    endTime = now + 10  # ten seconds to run loop
    while now < endTime:
        sleep(1)  # do something that takes some time (one second here)
        now = time()
        print now

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      Or depending on your goal you could try something like this:

      Code:
      from time import time
      startTime = time()
      for i in range(100):
                t = time() - startTime
                print i,t

      Comment

      Working...