C# 测试软件运行时间
时间: 2023-12-05 16:41:35 浏览: 92
测试程序的运行时间
4星 · 用户满意度95%
以下是两种C#测试软件运行时间的例子:
1.获取程序运行时间
```csharp
DateTime startTime = DateTime.Now;
// your code here
TimeSpan runTime = DateTime.Now.Subtract(startTime);
Console.WriteLine("程序运行时间为:" + runTime.TotalMilliseconds + "毫秒");
```
2.获取线程运行时间
```csharp
DateTime startTime = DateTime.Now;
TimeSpan userTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
// your code here
TimeSpan runTime = DateTime.Now.Subtract(startTime);TimeSpan threadTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(userTime);
Console.WriteLine("程序运行时间为:" + runTime.TotalMilliseconds + "毫秒");
Console.WriteLine("线程运行时间为:" + threadTime.TotalMilliseconds + "毫秒");
```
阅读全文