3

I need to know how much this particular line of code loads my cpu when whole program is executed:

cap.set(cv.CAP_PROP_POS_FRAMES,random_frame)

This line is part of a certain program. But I'm only interested in how this particular line loads the cpu. I don't know exactly how to measure it.

To be more precised, this is my full code:

import cv2 as cv
import random

cap = cv.VideoCapture('file_name.avi')

random_frame = random.randint(1,99999)

cap.set(cv.CAP_PROP_POS_FRAMES,random_frame)

ret, frame = cap.read()

cv.imshow("random_frame",frame)

while cap.isOpened():
    if cv.waitKey(1) == ord('q'):
        break
    
cap.release()
cv.destroyAllWindows()

Windows 7 operating system

Sebastian
  • 31
  • 2
  • You could use the *perf_counter()* function from the *time* module although that may not give you the granularity you need – DarkKnight Sep 04 '22 at 17:25
  • Python is slow enough that it might barely be meaningful to time a function call that just sets one property, but modern CPUs have multiple instructions running at once so overall performance isn't always the sum of separate times. And timing overhead is pretty significant even compared to CPython interpreting one function call. Even if you could put some low-level timing like x86 `rdtsc` into the Python interpreter itself, which you can't easily. So your timing code also has to be in Python. – Peter Cordes Sep 04 '22 at 17:43
  • See [Idiomatic way of performance evaluation?](https://stackoverflow.com/q/60291987) - you might be able to put that in a loop and time the whole loop, although that would hide any cache-miss costs that might happen in real usage. OTOH, if this is something your real code does a lot, caches will probably be hot. – Peter Cordes Sep 04 '22 at 17:44
  • since you aren't asking about OpenCV specifically, but about **how to profile your code**, I am not sure this should stay tagged as "OpenCV" – Christoph Rackwitz Sep 05 '22 at 17:05

1 Answers1

0
import multiprocessing as mp
import psutil
import random
import cv2 as cv
import random

cap = cv.VideoCapture ('file_name.avi')

random_frame = random.randint (1, 99999)


def monitorFunction():
  cap.set (cv.CAP_PROP_POS_FRAMES,random_frame)

def monitor (target):
    worker_process = mp.Process(target=target)
    worker_process.start()
    p = psutil.Process(worker_process.pid)

    # log cpu usage of `worker_process` every 10 ms
    cpu_percents = []
    while worker_process.is_alive():
        cpu_percents.append(p.cpu_percent())
        time.sleep(0.01)

    worker_process.join()
    return cpu_percents

cpu_percents = monitor(target=monitorFunction)
print (cpu_percents)

This should work.

  • Thank you, thats very helpful. But: 1. Most of the times I'm getting error: `NoSuchProcess: process no longer exists (pid=12345)` while running program; 2. How should I interpret the results? Because I'm getting values betwen 0.0 and even 450.0 as a result of program execution. I guess that are not values of cpu usage in percentage, as I expected – Sebastian Sep 07 '22 at 11:26