what is the unit of timeit() from python3 timeit module function [duplicate] - python

This question already has an answer here:
What unit of time does timeit return?
(1 answer)
Closed 4 years ago.
import time
import timeit
start = timeit.timeit()
time.sleep(5)
end = timeit.timeit()
time_elapsed = end - start
print(end)
print(time_elapsed)
print(start)
Outputs:
0.018255482330552297
-0.00033663523232263515
0.018592117562874932

As the documentation clearly says:
This… returns the time it takes to execute the main statement a number of times, measured in seconds as a float.
Meanwhile, notice that this is the elapsed time to run the main statement—that is, nothing at all, in your case. It's not a timestamp or anything like that. So subtracting end - start doesn't give you anything useful—it's the difference in elapsed time for two different runs of a no-op.

Related

A general programming question: Is there a way to trigger a function every x seconds without remembering the last time it was triggered? [duplicate]

This question already has answers here:
How do I get a Cron like scheduler in Python?
(9 answers)
Closed last month.
Sorry if this is a basic question, but I want to have a function trigger every fixed amount of time, say 2 seconds. In general I would do something like:
lifetime = getCurrentTime
if (lifetime - lastTime > triggerTime)
doTheThing()
lastTime = lifetime
end
This normally works fine, but I'm working on a program where each object stores its own lifetime, and there could be hundreds of objects at once. I'm wondering if there's a way that each object wouldn't need to also remember its own lastTime -- I was thinking something with rounding or modulo, but even then you'd still need to remember how many times the function had triggered before.
I'm wondering if anyone knows of a good general way to have a trigger function every x seconds without having to remember the last time it triggered or how many times it's triggered?
Have you tried using sched yet? Here's a task done every 5 seconds to get you started:
import sched
import time
def doTheThing(runnable_task, every):
runnable_task.enter(every, 1, doTheThing, (runnable_task, every))
print("The thing...")
task = sched.scheduler(time.time, time.sleep)
every = 5
task.enter(every, 1, doTheThing, (task, every))
task.run()

run python program every 15 minutes when seconds are 0 [duplicate]

This question already has answers here:
Start a Function at Given Time
(9 answers)
Closed 1 year ago.
I have been writing this code and the time taken for the execution is about 3-4 seconds and I want it to run every 15 minutes I have been using this code
while True:
if datetime.datetime.now().minute % 15 == 0:
...
time.sleep(60)
now I cannot start the code to run exactly when the seconds on the clock are at 0 so I want the code to run exactly when the seconds are zero.
Thanks in advance
If you'll think about it - you're suspending your code for 60 seconds on time.sleep(60) but you're not considering the time it takes for the code to execute each time, which creates a time difference.
You could calculate the difference in time for the next execution to happen instead of constant 60
BTW, If you want to execute a code repeatedly you could use crontab to execute your scripts for you, it's simple and on-time so you won't need to worry about timings

Python while loop be executed in time interval [duplicate]

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 2 years ago.
I wanted to use while loop to keep executing my script over and over but there is a problem as my code calls an API that doesn't allow so many calls in small time so I wanted to make the while loop be executed at time interval so I tried this code
from threading import Timer
def myfunc():
some code
while True:
t = Timer(1.0, myfunc)
t.start()
but it doesn't work, so is there any other way to do it correctly?
Use the time module:
import time
def myfunc():
some code
while True:
myfunc()
# unit is in second. Example below wait for 1 second before continuing
time.sleep(1)

Using sleep() to create a function that consistently loops every second [duplicate]

This question already has answers here:
How to repeatedly execute a function every x seconds?
(22 answers)
Closed 4 years ago.
I have a program that needs to execute every second. However I am concerned that the code would add a slight delay in turn causing it delay slightly longer then intended. Sample code:
while True:
print(time)
sleep(1)
In my case I will be adding more complicated function calls in this loop and am concerned that they will mess with my timer. Should I even be worried, and or is there another way for me to ensure this function loops every second?
You can use this:
import threading
def scheduleFunc():
threading.Timer(1.0, scheduleFunc).start()
print(time)
Or use this:
import sched, time
scheduled = sched.scheduler(time.time, time.sleep)
def scheduleFunc(sc):
print(time)
scheduled.enter(60, 1, scheduleFunc, (sc,))
scheduled.enter(60, 1, scheduleFunc, (scheduled,))
scheduled.run()

Timing python programs [duplicate]

This question already has answers here:
How do I get time of a Python program's execution?
(38 answers)
Closed 4 years ago.
So I am trying to see how long it takes my program to run, and the solution I came up with is:
import datetime
time1 = datetime.datetime.now()
[program code]
time2 = datetime.datetime.now()
print(time2 - time1)
I want to know if this is an efficient/correct solution, because all the guides I could find on the internet would use modules such as timeit, etc.
I would appreciate any feedback.
Yes, that works. However, another solution is:
import time
start = time.time()
# do stuff
print(time.time() - start)
You could also use time.clock() instead of time.time()

Categories