Stopwatch计时器、秒表 C#

.NET2.0也提供了这样一个秒表:Stopwatch类,它可以比较精确地测量时间。
速度测试:
软件的性能和可测性是一个复杂的主题。要确保应用程序能够满足用户的期望,就需要在开发周期内考虑它的性能和可测性。这在设计阶段至关重要,一个糟糕的设计几乎肯定会导致糟糕的用户体验。然而,仅仅有好的设计也不能保证程序能够高效地运行,最终代码的质量同样重要。
量度一个运行时间较长的例程相当简单。如果一个过程会持续几分钟,只要一块腕表就可以记录它的时间了。比如一个执行时间为两分钟的过程,10%的改善能够节省12秒,这是很容易去确定的。
而如果要测量一个非常短暂的过程,就要考虑更好的精确性了。比如有一些很小的例程,它们的运行时间可能只有千分之一秒,但会被调用100万次,这样的累积效果就明显了。在.NET framework的先前版本中,需要使用Windows API函数,而在.NET framework 2.0中,微软引入了Stopwatch(它就是我们的秒表)类来简化时间的量度任务。
Stopwatch类:
使用Stopwatch类来量度时间非常简单。跟现实生活中的秒表一样,这个类的对象也能够对计数器进行开始、停止、归零(重置)操作,不过它可比一般的秒表精确多了,它能够精确到微秒(也就是百万分之一秒)。
示例代码:
要演示Stopwatch的使用还是来段代码吧。下面是一个控制台应用程序,它将1到100万之间的所有整数累加:


usingSystem;

namespaceStopWatchClass
{
classProgram
{
staticvoidMain(string[] args)
{

longtotal=0;

for(inti=1; i<=10000000; i++)
{
total
+=i;
}
}
}
}


添加Stopwatch对象:
Stopwatch类位于System.Diagnostics命名空间。下面是添加对象后的代码:

usingSystem;
using System.Diagnostics;

namespaceStopWatchClass
{
classProgram
{
staticvoidMain(string[] args)
{
Stopwatch timer
= new Stopwatch();
longtotal=0;

for(inti=1; i<=10000000; i++)
{
total
+=i;
}
}
}
}


控制Stopwatch对象:
Stopwatch提供了几个方法用以控制Stopwatch对象。Start方法开始一个计时操作,Stop方法停止计时。此时如果第二次使用 Start方法,将继续计时,最终的计时结果为两次计时的累加。为避免这种情况,在第二次计时前用Reset方法将对象归零。这三个方法都不需要参数。代码是:

usingSystem;
usingSystem.Diagnostics;

namespaceStopWatchClass
{
classProgram
{
staticvoidMain(string[] args)
{
Stopwatch timer
=newStopwatch();
longtotal=0;

timer.Start();
for(inti=1; i<=10000000; i++)
{
total
+=i;
}

timer.Stop();

}
}
}


读取Stopwatch结果:
在结束计时后下一步就是读取计时结果了。Stopwatch类提供了以下属性:

  • Elapsed:返回一个TimeSpan对象,表示计时时间间隔;
  • ElapsedMilliseconds:返回计时经过的微秒数,精确度稍差,适合于稍长一点的计时;
  • ElapsedTicks: 返回计时经过的计时器刻度(timer tick)数。计时器刻度是Stopwatch对象可能的最小量度单位。计时器刻度时间的长度由特定的计算机和操作系统确定。Stopwatch对象的 Frequency静态字段的值表示一秒所包含的计时器刻度数。注意它与TimeSpan的Ticks属性所用的时间单位的区别。
应当根据计时任务的情况选择其中的一个属性。在我们的示例程序中,Elapsed属性提供了需要的精确度,用它来输出经过的微秒数。这也是TimeSpan的最高精确度了。
下面是最终的程序代码:


usingSystem;
usingSystem.Diagnostics;

namespaceStopWatchClass
{
classProgram
{
staticvoidMain(string[] args)
{
Stopwatch timer
=newStopwatch();
longtotal=0;

timer.Start();
for(inti=1; i<=10000000; i++)
{
total
+=i;
}

timer.Stop();

decimal micro = timer.Elapsed.Ticks / 10m;
Console.WriteLine(
"Execution time was {0:F1} microseconds.", micro);
}
}
}


另外,使用IsRunning属性可以查看一个Stopwatch实例是否正在计时,使用StartNew方法可以开始一个新的计时器。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
namespace StopWatch { public partial class Form2 : Form { DateTime examtime; DateTime nowtime; DateTime t = DateTime.Now; int add = 0; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { timer1.Enabled = false; examtime = new DateTime(1, 1, 1, 0,0, 0); labTime.Text = "0" + examtime.ToLongTimeString(); if (t.Hour < 10) labT.Text = "0" + DateTime.Now.ToLongTimeString(); else labT.Text = DateTime.Now.ToLongTimeString(); } private void button2_Click(object sender, EventArgs e) { if (add < 1 ) { starttime(); } else { MessageBox.Show("记录已满,请清除记录后再开始记录!"); } } private void button1_Click(object sender, EventArgs e) { if (timer1.Enabled != false) { Add(); } if (add >= 5) { MessageBox.Show("对不起!最多只能记录4条!!"); timer1.Enabled = false; } } private void button3_Click(object sender, EventArgs e) { ClearTime(); add = 0 - 1; Add(); stoptime(); } private void button4_Click(object sender, EventArgs e) { Application.Exit(); } private void timer1_Tick(object sender, EventArgs e) { examtime = examtime.AddSeconds(1); if (examtime.Hour < 10) labTime.Text = "0" + examtime.ToLongTimeString(); else labTime.Text = examtime.ToLongTimeString(); } private void timer2_Tick(object sender, EventArgs e) { if(t.Hour<10) labT.Text = "0"+DateTime.Now.ToLongTimeString(); else labT.Text = DateTime.Now.ToLongTimeString();nowtime = nowtime.AddSeconds(1); } private void Add() { add = add + 1; switch (add) { case 1: textBox1.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 2: textBox2.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 3: textBox3.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 4: textBox4.Text = "0" + examtime.ToLongTimeString(); Program.hash.add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text); timer1.Enabled = true; timer1.Enabled = false; break; case 5: default: break; } } private void ClearTime() { examtime = new DateTime(1, 1, 1, 0, 0, 0); labTime.Text = "0" + examtime.ToLongTimeString(); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox1.Text = "0" + examtime.ToLongTimeString(); textBox2.Text = "0" + examtime.ToLongTimeString(); textBox3.Text = "0" + examtime.ToLongTimeString(); textBox4.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = false; } private void starttime() { timer1.Enabled = true; } private void stoptime() { timer1.Enabled = false; } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void textBox3_TextChanged(object sender, EventArgs e) { } private void textBox4_TextChanged(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void textBox5_TextChanged(object sender, EventArgs e) { } private void labTime_Click(object sender, EventArgs e) { } private void timeshow_Click(object sender, EventArgs e) { Form3 frm = new Form3(); frm.Show(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值