Suppose I want to measure the time that a certain piece of code takes. For that I would normally do something like this
clock_t startTime = clock();
//do stuff
//do stuff
//do stuff
//do stuff
float secsElapsed = (float)(clock() - startTime)/CLOCKS_PER_SEC;
What if the program is multithreaded and context switches occur within the part which I want to measure? How would I measure the time that my code takes to execute excluding time spent on other threads? Even if there are tools that do it, I would very much like to know how they're doing it.
CLOCK_THREAD_CPUTIME_ID
- this in theory (I've not tested it myself) should report the time specific to that thread. – DisillusionizeQueryPerformanceCounter
? AFAIK, you have to pin the thread to a specific CPU to get best results though (SetThreadAffinityMask
) - not sure how the windows scheduler works, but on linux you can control which cores the scheduler uses and which it can't so you can guarantee that the core you're using will not be used by the scheduler (and therefore your thread interrupted)... – DisillusionizeGetThreadTimes
API funciton may come handy, unless you're measuring CPU cycles. – Cobol