Python get script runtime in raunded minutes [duplicate] - python

This question already has answers here:
How to round numbers
(2 answers)
Closed 5 years ago.
I like to get the run time of the script in rounded minutes.
I put this at the start
import time
start_time = time.time()
And this at the end
print ("My program took", time.time() - start_time, "to run")
so I get output value in seconds like 246.60637378692627 seconds
I like to get instead 4,11 minutes

Just divide your seconds by 60 and then use the round function:
time_taken = round((time.time() - start_time)/60.0, 2)
print ("My program took", time_taken, "to run")

Related

End the execution of a script after a certain amount of time - Python [duplicate]

This question already has answers here:
How would I stop a while loop after n amount of time?
(11 answers)
Closed 9 months ago.
Good evening everyone. Guys I'm implementing a function that processes network packets. At the moment I'm using a while loop so that the function is always running, but I would like to set a time in ms and at the end of that time the loop ends. Can you give me a tip? A snippet of my loop so far:
while True:
status = pcap.loop(pd, 0, processing_pkts,
ct.cast(ct.pointer(packet_count), ct.POINTER(ct.c_ubyte)))
if status < 0:
break
Thanks for any tips!!!
import time
end_time = time.time() + 60 * 10 //this will run for 600 seconds
while time.time() < end_time:
// do your work

Convert the difference between datetime.datetime field to seconds [duplicate]

This question already has answers here:
Python - Calculate the difference between two datetime.time objects
(4 answers)
Closed 2 years ago.
I have a piece of code where I have calulated the time taken for the algorithm to run through datetime as below.
begin_time = datetime.datetime.now()
#some algo code
datetime.datetime.now()-begin_time
I expected the output in seconds but it is in some different form as below:
0.000359
I am pretty sure that these are not seconds. How do i convert it to seconds?
Try this
import time
tic = time.perf_counter()
#Your code here
toc = time.perf_counter()
print(f`'Your program took {toc - tic:0.2f} seconds to run')
The difference is calculated in hours so you just need to multiply it by 3600 to convert it to seconds.

Comparing Time - Python [duplicate]

This question already has answers here:
How do I check the difference, in seconds, between two dates?
(7 answers)
Closed 4 years ago.
I have a function where I read the time from a file. I put this into a variable. I then subtract this value from the current time which most of the time will give me a value around .
My problem is im not sure how to check if this value which I attach to a variable is greater than say 20 seconds.
def numberchecker():
with open('timelog.txt', 'r') as myfile:
data=myfile.read().replace('\n','')
a = datetime.strptime(data,'%Y-%m-%d %H:%M:%S.%f')
b = datetime.now()
c = b-a (This outputs for example: 0:00:16.657538)
d = (would like this to a number I set for example 25 seconds)
if c > d:
print ("blah blah")
The difference which you're getting is a timedelta object.
You can just use c.seconds to get the seconds.
if c.total_seconds() > datetime.timedelta(seconds=d):
print ("blah blah")

Breaking down seconds in a day [duplicate]

This question already has answers here:
How do I convert seconds to hours, minutes and seconds?
(18 answers)
Closed 6 years ago.
I'm trying to breakdown a users integer input into hours, minutes, and seconds for a 24 hour period but having issues with going from pseudo code to actual code past the first hour equation. I want the final output to be in X hours, Y minutes, and Z seconds:
day = 86400
hour = 3600 #1 hour in a day * 60min * 60 sec
minute = 60
number = input("Choose a number between 0 and 86400: ")
while number != 0:
if number > 0:
newNumber = number / hour
number = newNumber
I'm teaching myself coding so I would love a simple approach to this problem... Am I even on the right track?
EDIT: I know there is a duplicate version of this question but that one simplifies things a bit too much (ironic) for me. I'm trying to learn by incremental steps but I do appreciate all the feedback
To convert a users input into X hours Y minute and Z seconds it depends on what they're inputting. If it's seconds, as it seems to be above, then do as the above says and take the seconds and divide it by 3600 for your hours, then subtract that number from the beginning number.
Also, it looks like you're using an infinite while loop, as you aren't prompting for input each time around.
If you want to do what you're trying to do I would suggest this:
number = input("Choose a number between 0 and 86400")
while(number != 0)
hours = number/3600
number = number-(hours*3600)
minutes = number/60
number = number-(minutes*60)
seconds = number
number = 0
print("The Time You Entered was " + str(hours) + " hours, " + str(minutes) + " minutes, and " + str(seconds) " seconds.")

How can I print "hello world" every 34 minutes in python? [duplicate]

This question already has answers here:
Executing periodic actions [duplicate]
(9 answers)
Closed 9 years ago.
How can I print "hello world" every 34 minutes in python? Right now I'm grabbing the system time, comparing it to my last recording time, looping around (which is a pain for 24 hour clocks), rinse and repeat. Is there a nice and easy way to do this.
To put simply, how would you loop code this code every 34 minutes:
print("Task to do every 34 minutes")
Use the time module:
from time import sleep
while True:
print("Task to do every 34 minutes")
sleep(60 * 34) # argument is seconds
This can be a solution:
import time
while True:
print "hello world"
time.sleep(2040)
Sleep takes argument as number of seconds so 60*34 = 2040.

Categories