I'm looking for a way to measure the cpu cycles a function call on a thread takes.
Example pseudo code:
void HostFunction()
{
var startTick = CurrentThread.CurrentTick; //does not exist
ChildFunction();
var endTick = CurrentThread.CurrentTick; //does not exist
var childFunctionCost = endTick - startTick;
}
void ChildFunction()
{
//Do whatever...
Thread.Sleep(3000);
//Do some more...
}
I don't want to use a Stopwatch or some other time measurement, because it would include any time that the thread is sleeping, which I do not want to measure. I only want to measure real work.
This measurement needs to work at runtime, as in my pseudo code, as the results are used to determine if the child function should be allowed to continue to run (my real case is a plugin-type architecture), so a profiling tool won't help me.
Based on the comment I provided above, consider the following code:
which produces the following result (on my older machine):
You can clearly see that the 3 seconds of sleeping is not being included. Hope this helps.
You can pinvoke QueryThreadCycleTime(). Check the link for details.
Some sample code: