请问如何计算出一段代码所需的执行时间!! 
本人以前看到过这样的问题  可是没留心
 只记得  要获得一段程序执行前和后的时间  并相减就能得到这段代码所需的执行时间
  可具体怎么实现的   就不记得了
 
           诚请指教!

解决方案 »

  1.   

    Use "TimeSpan" ,
    Sample code as follows:
    DateTime dtStart = DateTime.Now;
    ...TimeSpan ts = DateTime.Now - dtStart;
    //ts.Ticks
      

  2.   

    //也可以使用Timer控件using System;
    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();
    } }
    }
      

  3.   

    能不能有一种比较精确的计算执行时间的啊
       我觉得有datetime精确度太低了   因为一般的程序执行都是毫秒级的
      

  4.   

    datetime 只能精确到秒......
      

  5.   

    是2.0的就推荐楼上的吧,可以精确到nanosecond
      

  6.   

    public long GetTime(int num)
            {
                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怎么转化为毫秒呢??
      

  7.   

    Use "TimeSpan" ,
    Sample code as follows:
    DateTime dtStart = DateTime.Now;
    ...TimeSpan ts = DateTime.Now - dtStart;
    //ts.Milliseconds
      

  8.   

    DateTime dtStart = DateTime.Now;
      这样就有问题啊   DateTime.Now取值不精确
      我要用datetime.now.ticks   可这是长整形....长整形怎么转化为毫秒呢
      

  9.   

    除以一万得到毫秒数,刚才说过了,是nanosecond