怎样能计算有一段程序运行了多长时间,程序运行完弹出messagebox即可,显示多少秒或几分几秒
怎样能计算有一段程序运行了多长时间,程序运行完弹出messagebox即可,显示多少秒或几分几秒
[解决办法]
TimeSpan可以算差
[解决办法]
- C# code
DateTime start=DateTime.Now;//这里写程序执行代码DateTime end=DateTime.Now;TimeSpan last=end-start;string result="执行了"+last.Minute+"分"+last.Second+"秒";
[解决办法]
搜一下Stopwatch这个类,就是秒表了。
[解决办法]
- C# code
//the start time DateTime startTime = DateTime.Now; Console.WriteLine(startTime); //the end time. DateTime stopTime = DateTime.Now; Console.WriteLine(stopTime); System.TimeSpan duration = stopTime - startTime; // 总共多少分钟 Console.WriteLine("minutes:" + duration.TotalMinutes); //总共多少秒钟 Console.WriteLine("seconds:" + duration.TotalSeconds);
[解决办法]
可以用Stopwatch活着TickCount,这里有个例子
http://blog.csdn.net/jinjazz/archive/2007/12/10/1927126.aspx
[解决办法]
2楼和4楼的朋友回答的很好,我这里有一个实际的例子:
- C# code
private void button1_Click(object sender, EventArgs e) //查询语句; { this.Cursor = Cursors.WaitCursor; //获取鼠标的形状,为沙漏形状 DateTime KaiShi = DateTime.Now; ChaXun_HZ(); DateTime Eend = DateTime.Now; TimeSpan XiangCha = Eend - KaiShi; string zhixing = XiangCha.Minutes + "分" + XiangCha.Seconds + "秒"; this.Cursor = Cursors.Default; //获取鼠标为正常形状 MessageBox.Show("此查询一共用时:"+zhixing,"提示"); }
[解决办法]
System.Diagnostics.Stopwatch