C#时间函数(程序运行计时,页面运行计时)
2010-09-30 17:58
435 查看
本文介绍两个C#时间函数。
1.DateTime
2.程序运行时间:(单位 : 毫秒)
1.DateTime
1 DateTime now = System.DateTime.Now; 2 now.ToString(); //显示: 2006/08/30 17:31:02 3 now.ToString("yyyy-mm-dd hh:MM:ss"); //显示: 2006-08-30 05:39:11 4 now.ToString("yyyy-mm-dd HH:mm:ss"); //显示: 2006-08-30 17:40:50 5 System.DateTime.MaxValue.ToString(); //显示: 9999/12/31 23:59:59 6 System.DateTime.MinValue.ToString(); //显示: 0001/01/01 0:00:00 7 now.ToLongDateString(); //显示: 2006年8月30日 8 now.ToLongTimeString(); //显示: 17:34:23 9 now.ToShortTimeString(); //显示: 17:34 10 now.ToString() + " " + now.Millisecond.ToString(); //显示: 2006/08/30 17:35:19 484 |
1 System.Diagnostics; //名称空间 2 int x = 0; 3 int nu = 0; 4 Stopwatch sw = new Stopwatch(); 5 sw.Start(); 6 //程序开始 7 for (int i = 0; i < 1000000; i++) 8 { 9 x += i; 10 } 11 //程序结束 12 sw.Stop(); 13 this.label1.Text += ",sum=" + x.ToString(); 14 MessageBox.Show(sw.ElapsedMilliseconds.ToString()); 3.计算一个页面执行时间: 在Global.asax.cs文件中增加以下代码: 1 protected void Application_BeginRequest(Object sender, EventArgs e) 2 { 3 Application["StartTime"] = System.DateTime.Now; 4 } 5 protected void Application_EndRequest(Object sender, EventArgs e) 6 { 7 System.DateTime startTime = (System.DateTime)Application["StartTime"]; 8 System.DateTime endTime = System.DateTime.Now; 9 System.TimeSpan ts = endTime - startTime; 10 Response.Write("页面执行所用时间:" + ts.Milliseconds + " 毫秒"); 11 } 12 |
相关文章推荐
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C# 时间(几个常用时间,程序运行计时,页面运行计时).txt
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- C#记录程序运行时间记录显示
- c#计算程序的运行时间
- Java当中计算关于程序运行时间的函数
- C#测量程序运行时间及cpu使用时间实例方法
- c语言中如何添加计时函数,统计程序执行的时间
- c# 获取当前运行程序文件,函数,行号
- C#获取程序运行时间
- 如何在C#中测试程序运行的时间
- c语言中如何添加计时函数,统计程序执行的时间
- linux下统计程序/函数运行时间(转)
- c++中记录程序运行时间的函数