当前位置:网站首页>C # use stopwatch to measure the running time of the program

C # use stopwatch to measure the running time of the program

2022-07-04 19:38:00 Robot automation control

Stopwatch Examples can be measured ⼀ Run at intervals ⾏ Time , It can also measure the total operation of multiple time intervals ⾏ Time . In a typical Stopwatch ⽅ In the case , First tune ⽤ Start ()⽅ Law , Then tune up ⽤ Stop() ⽅ Law , Finally make ⽤ Elapsed Attribute check operation ⾏ Time .

TimeSpan The value can be expressed as [-]d.hh:mm:ss.ff, Where the minus sign is optional , It indicates a negative time interval ,d The component represents days ,hh For hours (24 hourly ),mm Represents minutes ,ss For seconds , and ff Is the fractional part of the second . namely , The time interval includes positive and negative days of the whole 、 Number of days and remaining less than one day , Or less than a day .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace Stopwatch Use 
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // Object instantiation 
        Stopwatch stopwatch = new Stopwatch();
        private void button1_Click(object sender, EventArgs e)
        {
            // The timer starts 
            stopwatch.Start();
            for(int i = 0; i < 800000000;i++)
            {
                int j = 0;
                j = j + 2;   
            }
            // The timer stops 
            stopwatch.Stop();
            // Determine whether the timing is in progress ,true For in progress 
            bool isrunning = stopwatch.IsRunning;
            // Get run time   millisecond 
            long times1 = stopwatch.ElapsedMilliseconds;

            TimeSpan times2 = stopwatch.Elapsed;
           double days = times2.TotalDays;
            double hours = times2.TotalHours;
            double minutes = times2.TotalMinutes;
            double seconds = times2.TotalSeconds;
           double milliseconds = times2.TotalMilliseconds;

            textBox1.Text += " Total operation time :" + times1 + "  millisecond ";
            textBox2.Text += " Total operation time :" + hours + "  Hours ";
            textBox3.Text += " Total operation time :" + minutes + "  branch ";
            textBox4.Text += " Total operation time :" + seconds + "  second ";
            textBox5.Text += " Total operation time :" + milliseconds + "  millisecond ";

        }
    }
}

  As can be seen from the above figure TimeSpan Timing time ratio StopWatch More accurate

原网站

版权声明
本文为[Robot automation control]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041806089770.html