0

I know the execution time for any python program shall depend on the OS and cannot be controlled by the User. But what I want is the program to go in sleep if the execution time is lower than necessary.

Let's say I have a python program which has a print statement at the end.

def foo():
    ...
    ...
    return(ans)

print(foo())

Using timeit I have evaluated the range of execution time taken for foo. Let it be from 0.8 seconds to 5.5 seconds. I choose the execution time of the complete script as 10 seconds to be on the safe side.

I want the program to add delay of 9.2 seconds before print statement if the execution of foo was completed in 0.8 seconds. Likewise a delay of 4.5 seconds if execution was completed in 5.5 seconds.

2
  • How precise do you need to be? Commented Mar 19, 2022 at 4:59
  • @ddejohn not much, a resolution of 0.1 seconds works. Commented Mar 19, 2022 at 5:04

2 Answers 2

1

You basically just have to sleep for the amount of time that is the difference between the maximum time and actual execution time. you could also make a general purpose decorator.

class padtime:
    def __init__(self, maxtime):
        self.maxtime = float(maxtime)

    def __call__(self, f):
        def _f(*args, **kwargs):
            start = time.time()
            ret = f(*args, **kwargs)
            end = time.time()
            delay = self.maxtime - (end - start)
            if delay > 0.0:
                time.sleep(delay)
            return ret
        return _f

@padtime(9.5)
def foo():
    ...
    return("Answer")

that could be applied to any function.

Sign up to request clarification or add additional context in comments.

Comments

0

You can measure the execution time of foo() using two calls to time.time(). You can then compute the amount of time to stall the execution of the program using the computed execution time of foo():

import time

def foo():
    ...

start_time = time.time()
foo()
end_time = time.time()
if end_time - start_time < 10:
    time.sleep(10 - (end_time - start_time))

Note that we use time.sleep() rather than a while loop that repeatedly checks whether enough time has elapsed, since busy waiting wastes resources.

4 Comments

Kinda silly to bring up "wasting resources" when time is a resource and OP wants to intentionally waste it but ¯\_(ツ)_/¯
works well within the resolution required. Thanks! @BrokenBenchmark
@ddejohn Yeah, but busy waiting is a general pattern one should avoid. I was going to comment that on your answer, but it was deleted before I could answer. (I wasn't the one who downvoted, for the record.)
@ddejohn However, time is a different resource from CPU cycles. A sleep allows another process to use those cycles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.