Accurate elapsed time measurement

Hi everyone. I am currently building a self-balancing robot, for which I need an accurate measurement of the time elapsed between loops in my c++ code. The elapsed time is in orders of microseconds, however, these measurements jump around a lot (hundreds of microseconds) which has a big effect on my RPM calculations for my motors. I am currently using the chrono library but as stated before, the measurements are not stable. I suspect it has something to do with stuff running in the background in Linux. Is there a way of measuring only the time elapse
d by the C++ program, independent of Linux? I am very new to Linux, so any suggestions will be greatly appreciated.

Hi. If your’e using a beaglebone then look into PRUs. You can measure microsecond intervals.
https://docs.beagleboard.org/latest/books/pru-cookbook/index.html

Regards, Mike.

That is how the kernel operates.

The more you have going on the greater the number of stuff running, the system will become less deterministic system.

$pstree

Longer the tree the more randomness.

You can either rip out stuff and in so doing possibly totally break the os.
Or, do a yocto build using core-image-minimal or core-image-base. Make your own layer and add in only the stuff you really need. Then when you pstree you will only see a few lines and the performance will increase.
TI has some RTOS, we have not worked with it so I cannot comment on its performance, it might be a better option.