Reputation: 339
I would like to run several tasks simultaneously. To be clear, I desire that they be simultaneous, not merely parallel -- at any point in time, I want every task to have consumed an equal number of CPU cycles, within some tolerance. How is this best done in .Net?
Upvotes: 0
Views: 259
Reputation: 127563
There are two ways to measure this, Wall Clock time and CPU time. Most people will likely tell you to use Stopwatch to measure the performance, but that may or may not be the number you want.
If all you care about is how long it took to execute in real time, use stopwatch, if you want to know how long the thread itself was running, this can be done with the Native API call GetThreadTimes. Here is a article on CodeProject that uses it. Note that you won't be able to use the TPL for this, you will need to use real Threads as the TPL it recycles threads so the times will not be the real execution times for the Task.
A second option is use QueryThreadCycleTime. This will not give you the time, but it will give you the number of cycles the current thread has been executing, you can do one at the start and one at the end and subtract the two numbers. Note again Task
reuses threads so that number may or may-not be reliable unless you use full Threads.
Be aware you can't just directly convert cycles->seconds
due to the fact that many processors (especially mobile processors) do not run at a fixed speed so there is no constant number you could multiply by to get the elapsed time in seconds. But if you are using a processor that does not vary its speed it then would be a simple math problem to get wall clock time from the cycles.
Upvotes: 1