当前位置:网站首页>C# 使用StopWatch测量程序运行时间
C# 使用StopWatch测量程序运行时间
2022-07-04 18:32:00 【机器人自动化控制】
Stopwatch 实例可以测量⼀个时间间隔的运⾏时间,也可以测量多个时间间隔的总运⾏时间。在典型的 Stopwatch ⽅案中,先调⽤ Start ()⽅法,然后调⽤ Stop() ⽅法,最后使⽤ Elapsed 属性检查运⾏时间。
TimeSpan 值可以表示为 [-]d.hh:mm:ss.ff,其中减号是可选的,它指示负时间间隔,d 分量表示天,hh 表示小时(24 小时制),mm 表示分钟,ss 表示秒,而 ff 为秒的小数部分。即,时间间隔包括整的正负天数、天数和剩余的不足一天的时长,或者只包含不足一天的时长。
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的使用
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//对象实例化
Stopwatch stopwatch = new Stopwatch();
private void button1_Click(object sender, EventArgs e)
{
//计时器启动
stopwatch.Start();
for(int i = 0; i < 800000000;i++)
{
int j = 0;
j = j + 2;
}
//计时器停止
stopwatch.Stop();
//判断计时是否正在进行,true为正在进行中
bool isrunning = stopwatch.IsRunning;
//获取运行时间 毫秒
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 += "总运行时间:" + times1 + " 毫秒";
textBox2.Text += "总运行时间:" + hours + " 小时";
textBox3.Text += "总运行时间:" + minutes + " 分";
textBox4.Text += "总运行时间:" + seconds + " 秒";
textBox5.Text += "总运行时间:" + milliseconds + " 毫秒";
}
}
}
从上图可以看出TimeSpan计时的时间比StopWatch更为精确
边栏推荐
- prometheus安装
- Download the first Tencent technology open day course essence!
- 牛客小白月赛7 I 新建 Microsoft Office Word 文档
- 明明的随机数
- Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
- 2014合肥市第三十一届青少年信息学奥林匹克竞赛(小学组)试题
- SSL证书续费相关问题详解
- 自由小兵儿
- ftp、sftp文件传输
- PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
猜你喜欢

“只跑一趟”,小区装维任务主动推荐探索

Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?

一文掌握数仓中auto analyze的使用

To sort out messy header files, I use include what you use

Don't just learn Oracle and MySQL!

BI技巧丨权限轴

升级智能开关,“零火版”、“单火”接线方式差异有多大?

Online text line fixed length fill tool

LeetCode第300场周赛(20220703)

在线文本行固定长度填充工具
随机推荐
性能优化之关键渲染路径
Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
函数式接口
1008 Elevator(20 分)(PAT甲级)
Build your own website (15)
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
Wechat reading notes of "work, consumerism and the new poor"
线上数据库迁移的几种方法
Shell programming core technology "three"
[uniapp] uniapp development app online Preview PDF file
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
反射(一)
页面元素垂直水平居中、实现已知或者未知宽度的垂直水平居中。
偏移量函数及开窗函数
在线文本行固定长度填充工具
安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
Leetcode ransom letter C # answer
FPGA时序约束分享01_四大步骤简述