Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure C code execution time (Linux)

I want to measure the execution time of a c-code segment using Linux. I take one timestamps at the beginning of the code segment and one at the end. But I don't know how to protect the code against IRQs and context switches to high prior tasks. The program runs in user space! The code segment is short so don't panic hosing the system.

Does anyone know an easy solution for this kind of protection?

like image 778
Fox0815 Avatar asked Oct 22 '22 11:10

Fox0815


1 Answers

You can use getrusage(2) to get the CPU time used, rather than just measuring real time. That should get you the answer you want without having to resort to funny business like blocking other programs from running.

like image 64
Carl Norum Avatar answered Oct 24 '22 10:10

Carl Norum