请问如何计算出一段代码所需的执行时间!!
本人以前看到过这样的问题 可是没留心
只记得 要获得一段程序执行前和后的时间 并相减就能得到这段代码所需的执行时间
可具体怎么实现的 就不记得了
诚请指教!
本人以前看到过这样的问题 可是没留心
只记得 要获得一段程序执行前和后的时间 并相减就能得到这段代码所需的执行时间
可具体怎么实现的 就不记得了
诚请指教!
解决方案 »
- 看caozhy版主的关于评论老赵文章的一些关于斐波那契数列的感想
- 07新版 Excel zip解包 后 为啥我没找到 sharedStrings.xml
- IList的使用
- 在這種情況下,怎麼過濾掉comboBox裡面的重復項...
- vs.net 2005 中还能不能象2003中一样拖拽数据库中的表?
- .net framework 2.0正式版下的程序,中文输入法总是被切换成全角,怎么办?
- 请问如果多个panel重叠在一起,怎么设置显示顺序
- 搜索引挚内核
- C# Bitmap 改变图片像素,不会失真
- 极急!我在Windows Forms桌面程序中添加了一个WEB引用(WEB服务),为何2000下正常,98下出错?
- 有关C#中求素数的问题
- 三层架构和三层数据库有关系吗。
Sample code as follows:
DateTime dtStart = DateTime.Now;
...TimeSpan ts = DateTime.Now - dtStart;
//ts.Ticks
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Example_1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmTickCounter : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblComputer;
private System.Windows.Forms.Label lblApplication;
private System.Windows.Forms.GroupBox grpElapsed;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Timer tmrTickTimer;
private System.ComponentModel.IContainer components;
private int compuTime; public frmTickCounter()
{
InitializeComponent();
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.grpElapsed = new System.Windows.Forms.GroupBox();
this.lblApplication = new System.Windows.Forms.Label();
this.lblComputer = new System.Windows.Forms.Label();
this.btnClose = new System.Windows.Forms.Button();
this.tmrTickTimer = new System.Windows.Forms.Timer(this.components);
this.grpElapsed.SuspendLayout();
this.SuspendLayout();
//
// grpElapsed
//
this.grpElapsed.Controls.Add(this.lblApplication);
this.grpElapsed.Controls.Add(this.lblComputer);
this.grpElapsed.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.grpElapsed.Location = new System.Drawing.Point(8, 8);
this.grpElapsed.Name = "grpElapsed";
this.grpElapsed.Size = new System.Drawing.Size(600, 120);
this.grpElapsed.TabIndex = 0;
this.grpElapsed.TabStop = false;
this.grpElapsed.Text = "开机时长";
//
// lblApplication
//
this.lblApplication.Location = new System.Drawing.Point(16, 72);
this.lblApplication.Name = "lblApplication";
this.lblApplication.Size = new System.Drawing.Size(576, 26);
this.lblApplication.TabIndex = 1;
//
// lblComputer
//
this.lblComputer.Location = new System.Drawing.Point(16, 32);
this.lblComputer.Name = "lblComputer";
this.lblComputer.Size = new System.Drawing.Size(576, 26);
this.lblComputer.TabIndex = 0;
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(488, 136);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(115, 26);
this.btnClose.TabIndex = 1;
this.btnClose.Text = "关闭";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// tmrTickTimer
//
this.tmrTickTimer.Enabled = true;
this.tmrTickTimer.Interval = 20;
this.tmrTickTimer.Tick += new System.EventHandler(this.tmrTickTimer_Tick);
//
// frmTickCounter
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(616, 173);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.grpElapsed);
this.Name = "frmTickCounter";
this.Text = "时钟";
this.Load += new System.EventHandler(this.frmTickCounter_Load);
this.grpElapsed.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmTickCounter());
} private void frmTickCounter_Load(object sender, System.EventArgs e)
{
compuTime = Environment.TickCount;
} private void tmrTickTimer_Tick(object sender, System.EventArgs e)
{
//Environment类提供有关当前环境和平台的信息以及操作它们的方法
long curTickValue = Environment.TickCount;
long difference = curTickValue - compuTime;
long computerHours, computerMinutes, computerSeconds;
long applicationHours, applicationMinutes, applicationSeconds;
//converting milliseconds into hours, minutes and seconds computerHours = (curTickValue / (3600 * 999)) % 24;
computerHours = (curTickValue / (3600 * 999)) % 24;
computerMinutes = (curTickValue / (60 * 999)) % 60;
computerSeconds = (curTickValue / 999) % 60;
applicationHours = (difference / (3600 * 999)) % 24;
applicationMinutes = (difference / (60 * 999)) % 60;
applicationSeconds = (difference / 999) % 60; this.lblComputer.Text = String.Format("本计算机已运行了 {0} 小时 {1} 分 {2} 秒", computerHours.ToString(), computerMinutes.ToString(),
computerSeconds.ToString()); this.lblApplication.Text = String.Format("本应用程序已运行了 {0} 小时 {1} 分 {2} 秒",
applicationHours.ToString(), applicationMinutes.ToString(),
applicationSeconds.ToString()); } private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
} }
}
我觉得有datetime精确度太低了 因为一般的程序执行都是毫秒级的
{
long dtStart = DateTime.Now.Ticks;
ArrayList aa = new ArrayList(); for (int i = 1; i < num; i++)
{
if (sss(i))
{
aa.Add(i);
}
}
int count = aa.Count;
string dd = aa[count - 1].ToString();
long dtEnd = DateTime.Now.Ticks;
long ts = dtEnd - dtStart;
return ts;
}
返回值ts怎么转化为毫秒呢??
Sample code as follows:
DateTime dtStart = DateTime.Now;
...TimeSpan ts = DateTime.Now - dtStart;
//ts.Milliseconds
这样就有问题啊 DateTime.Now取值不精确
我要用datetime.now.ticks 可这是长整形....长整形怎么转化为毫秒呢