0

My code is used to make a distance estimation of a especific target. I need to know how long it takes to run the code, but I'm not sure when I should start and end the timer, since it's a loop. I tried something, but in my opinion the result is not really accurate, since it is taking about 60 ms, which is way faster than I was expecting. Can you help me?

In my code, I start the time benchmark after the loop starts:

while True:
    #Start dBenchmark
    start = time.time()

And I stop the time benchmark right after the distance of the target is already estimated.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Matheus
  • 3
  • 2
  • 1
    Does this answer your question? [Is there any simple way to benchmark Python script?](https://stackoverflow.com/questions/1593019/is-there-any-simple-way-to-benchmark-python-script) – Michael Cao Aug 09 '23 at 19:11

1 Answers1

0

The problem with timing your code is that you're measuring the execution time of each individual loop iteration rather than the overall time taken for the loop to execute a specific number of iterations. This can introduce inaccuracies, primarily if any overhead is related to starting and stopping the timer or other tasks/processes on your system interfere.

To get a more accurate estimation of the time it takes to run your code, consider the following approaches:

Time a large number of iterations

Instead of timing each loop iteration (or one distance calculation), time the code over many iterations (calculations) and divide by the number of iterations to get an average time per iteration (calculation).

num_iterations = 10000  # or any large number
start = time.time()

for _ in range(num_iterations):
    # Your distance estimation code here

end = time.time()
total_time = end - start
average_time_per_iteration = total_time / num_iterations

Use timeit - dedicated tool for benchmarking small pieces of code

Results from timeit is often more accurate for the following reasons according to this answer: time.time vs. timeit.timeit

  • It repeats the tests many times to eliminate the influence of other tasks on >your machine, such as disk flushing and OS scheduling.
  • It disables the garbage collector to prevent that process from skewing the >results by scheduling a collection run at an inopportune moment.
  • It picks the most accurate timer for your OS, time.time or time.clock in >Python 2 and time.perf_counter() in Python 3. See timeit.default_timer.

Here's an example of how to use timeit.

import timeit

def estimate_distance():
    # Your distance estimation code here
    pass


num_iterations = 10000  # or even bigger number
time_taken = timeit.timeit(estimate_distance, number=num_iterations)
average_time_per_iteration = time_taken / num_iterations
miloserdow
  • 1,051
  • 1
  • 7
  • 27
  • I create an array to save the time taken for it loop iteration and use calculate the mean of the array, does it work? My main doubt is about the momento to start and end timer. I was not sure if I should start the code right after de loop starts, of after the ret, frame - cap.read() for example. – Matheus Aug 09 '23 at 19:38
  • If you give it a sufficient number of iterations, it should not matter this much for the average where exactly you start/stop the timer. As long as there're no setup code or some huge long unrelated workloads (e.g. reading files). Regarding counting the time to start/stop the timer itself, if you use `timeit`, it should take care of it. – miloserdow Aug 09 '23 at 19:45