1import time
2tic = time.perf_counter() # Start Time
3your_program() # Your code here
4toc = time.perf_counter() # End Time
5# Print the Difference Minutes and Seconds
6print(f"Build finished in {(toc - tic)/60:0.0f} minutes {(toc - tic)%60:0.0f} seconds")
7# For additional Precision
8print(f"Build finished in {toc - tic:0.4f} seconds")
1import time
2start = time.time()
3# do something
4duration = time.time() - start
1import time, os
2
3seconds_to_go_for = 10 # How long the timer will go for
4current_time = int(time.time()) # Gets the time before the timer starts
5
6def clear():
7 if os.name == "nt":
8 os.system("cls") # Clear function, to avoid spam. Source: geeksforgeeks.org
9 else:
10 os.system("clear")
11
12while True:
13 time_now = int(time.time()) # Gets time during the timer's running
14 if time_now >= current_time + seconds_to_go_for: # Checks if enough time has passed
15 break # Stops loop if so
16
17 print(f"Seconds passed: {time_now - current_time}") # Prints how much time has passed
18 clear()
19print("The timer has ended")
1def hello():
2 print "hello, world"
3
4t = Timer(30.0, hello)
5t.start() # after 30 seconds, "hello, world" will be printed
6
1import time
2import sys
3
4time_start = time.time()
5seconds = 0
6minutes = 0
7
8while True:
9 try:
10 sys.stdout.write("\r{minutes} Minutes {seconds} Seconds".format(minutes=minutes, seconds=seconds))
11 sys.stdout.flush()
12 time.sleep(1)
13 seconds = int(time.time() - time_start) - minutes * 60
14 if seconds >= 60:
15 minutes += 1
16 seconds = 0
17 except KeyboardInterrupt, e:
18 break
1timer = threading.Timer(interval, function, args = None, kwargs = None)
2timer.start()