Create A Stopwatch (not A Countdown Or Timer) Without Using Tk Python
I am trying to make a stopwatch function for my module. It is to record the time that my module is called to be imported to the time that it is fully imported. The reason I am tryi
Solution 1:
If you're looking to time a snippet, the time.time()
method is what you're after. Try this:
startTime = time.time()
#long running code here
print"Time taken {}".format(time.time() - startTime)
Also take a look at timeit.
If an actual countdown is what you're after, you could try sleeping for n seconds until you get to 90, like so:
timeTotal = 0
timeResolution = 0.5
waitUntil = 90while timeTotal < waitUntil:
time.sleep(timeResolution)
timeTotal += timeResolution
print"\r{} seconds left".format(waitUntil - timeTotal),
Solution 2:
If a line of data is enough to showing your stopwatch, how about to rewrite a line on console by using 'carriage return' character "\r"?
Following code is simple countdown timer displays 100 to 0 at a fixed (not scrolled) line.
import sys, timefor num in range(100,0,-1):
sys.stdout.write("\r \r%d" % num)
sys.stdout.flush()
time.sleep(1)
The point is, write white spaces at first then write data you want.
In above code, "\r " is white spaces part, and "\r%d" is data part.
Post a Comment for "Create A Stopwatch (not A Countdown Or Timer) Without Using Tk Python"