To find out how long the program has been running?

Asked 2 years ago, Updated 2 years ago, 97 views

You want to create a program that runs only for 3 seconds if you type 3 before you run the program, and for 5 seconds if you type 5. How do I find out how many seconds have passed since the program started?

python time

2022-09-21 21:23

1 Answers

From Current Time - Program Start Time This is the easiest way.

import time
start_time = time.time()
#Code
print("--- %s seconds ---" % (time.time() - start_time))

It's using something.

For example, the following code is

import time
start_time = time.time()

#####
mylist = []
for i in range(0,10000000):
    mylist.append(i)
#####

print("--- %s seconds ---" % (time.time() - start_time))

Output: --- 2.8817028999328613 seconds ---


2022-09-21 21:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.