简体   繁体   中英

How to calculate execution time of c# application

How to calculate the execution time of c# application. I am having c# windows application in which i need to calculate the execution time and i dont have any idea where i have to proceed this. Can anyone please help me?

use Stopwatch of System.Diagnostics

static void Main(string[] args)
{
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    Thread.Sleep(10000);
    stopWatch.Stop();
    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value.
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
    Console.WriteLine("RunTime " + elapsedTime);
}

You could, for instance, just use DateTime.Now before and after your execution and then subtract the milliseconds:

DateTime then = DateTime.Now;

// Your code here

DateTime now = DateTime.Now;

Console.WriteLine(now.Millisecond - then.Millisecond);

Use the Benchmarking Made Easy library

A very easy and straightforward way is to use Jon Skeets Benchmarking Made Easy in C# toolset. Even with StopWatch, you still find yourself writing a lot of code around each bit you want to benchmark.

The benchmark toolset makes this trivial: you just hand it one or more functions and give them variable input and they will run until finished. The results for each function can then be introspected or printed to screen.

写一个静态类并在静态方法中编写上面的代码...在你想要使用计时器的地方调用该方法

您可以使用内置的分析器,它可以在主菜单下的VS2010 Premium和Ultimate中使用 - >分析 - > Profiler

using system.diagnostic; 

class Program {

  static void main(String[] args){

    Stopwatch Timer = new Stopwatch();

    //here is your code

    Timer.Stop();

    Console.Writeline("Time Taken:" +Timer.Elasped);

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM