There are many examples of how to get the elapsed time for a method but I
can't find any example of how to measure the amount of CPU consumed by my
thread in a method. Elapsed time in a multithreaded application
particularly
on a single processor CPU is not very useful.
Is there a way to measure this?
I don't think there's an easy way to mearure this. Certainly not using .Net.
Using the System.Diagnostics.ProcessThread class gives you a decent view
into the thread itself. For example, it has the PrivilegedProcessorTime,
Thread Affinity Mask, and so forth for the thread.
Also present is the ThreadId, which (if I remember right) is the Win32
Native Thread Id. You can use this to call into the Win32 threading API's
and get whatever information you need.
The only profiler that I've used that would easily give you what you want
are the Intel Profilers for .Net. I believe you can download trial versions
of them from the Intel web site. These profilers give you a huge amount of
data about your threads - L1, L2, L3 cache misses, instruction branch
mispredictions, and all the other good stuff you generally never want to
know about.
Don't forget though that .Net threads are not mapped 1:1 to native threads.
Unless your threads are using "Threading.Thread.BeginThreadAffinity" to make
sure they're tied to a real thread for the duration of an operation, you may
get some results that are confusing. (This hasn't come up for me before, so
I'm not sure if it's a real problem or not...)