C# 测试代码运行时间

一、新建一个控制台程序项目Test.exe

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace CodeRunTimeTest
 7 {
 8     public class Program
 9     {
10         static void Main(string[] args)
11         {
12             if (args.Length > 0)
13             {
14                 Console.WriteLine("代码运行时间为:" + args[0]);
15             }
16             else
17             {
18                 Console.WriteLine("没有输入时间!!!");
19             }
20             Console.ReadLine();
21         }
22     }
23 }

二、在工作项目代码中添加

1 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
2 sw.Start();
3 
4 /*测试代码*/
5 
6 sw.Stop();
7 System.Diagnostics.Process.Start(Application.StartupPath + "\test.exe ", sw.Elapsed.ToString());

三、结果显示(不影响工作项目的运行)

 

原文地址:https://www.cnblogs.com/ding2011/p/3359225.html