What is the best way to measure how long code takes to execute?

后端 未结 10 1240
梦谈多话
梦谈多话 2020-12-08 04:04

I\'m trying to determine which approach to removing a string is the fastest.

I simply get the start and end time a

10条回答
  •  不思量自难忘°
    2020-12-08 04:44

    I'll have to recommend the highly profiler included in Visual Studio Team Suite or Development Edition (or the upcoming Visual Studio 2010 Premium or Ultimate is even better) as the best way. It is highly configurable, extremely powerful, dead simple to use, very fast, and works with both native and managed code. I'm not familiar with the workflow of ANTS, but that appears to be another option. Without a doubt using a profiler is the only option for a developer that is concerned with their application's performance. There is no substitute, and you really can't take any commercial developer working on performance that would pass up a profiler seriously.

    However, if you are interested in measurements for one of the following, you may not have access such a profiler, in which case the Stopwatch class can form a basis for your measurement technique:

    • A student or hobbyist interested in performance of their projects (a commercial profiler may be out of reach for financial reasons)
    • In a publicly released application, you may want to time sections of code that execute on the UI thread and report statistics to may sure the operations never cause noticeable delays for any of your users. The Office team used a method like this with enormous success (Outlook 2007 SP2 anyone?), and I know the Visual Studio team has this code in at least the 2010 release.

提交回复
热议问题