Precisely measure execution time of code in thread (C#)

前端 未结 2 1766
长发绾君心
长发绾君心 2021-02-13 12:44

I\'m trying to measure the execution time of some bits of code as accurately as possible on a number of threads, taking context switching and thread downtime into account. The a

2条回答
  •  一向
    一向 (楼主)
    2021-02-13 13:07

    This can be done with the Native API call GetThreadTimes. Here is a article on CodeProject that uses it.

    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.

    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.

提交回复
热议问题