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

前端 未结 2 1759
长发绾君心
长发绾君心 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:14

    You can use Stopwatch.Start() and Stopwatch.Stop() methods to pause/continue time measurement, it does not reset Elapsed/ElapsedMilliseconds value so perhaps you can leverage this.

    Regarding thread context switches - I believe there are no ways to handle it in managed code so this is not possible to exclude time when thread was suspended

    EDIT:

    An interesting article with benchmarks: How long does it take to make a context switch?

提交回复
热议问题