System.Diagnostics.Stopwatch

http://blog.csdn.net/sky_zt/article/details/5950932

#if DEBUG
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
#endif

/==============中间各种程序====================//

#if DEBUG
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        string elapsedTime = String.Format(fac + ":{0:00}:{1:00}:{2:00}.{3:00}<br />", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
        //Response.Write(elapsedTime);
#endif

转载于:https://www.cnblogs.com/GerryGe/archive/2012/03/26/2417976.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化代码:public static string ReadRegister(SerialPort sp, string registerAddr, int numberOfPoints, string type) { try { sp.DiscardOutBuffer(); sp.DiscardInBuffer(); string value = null; byte[] readbytes = Read(registerAddr, numberOfPoints); sp.Write(readbytes, 0, readbytes.Length); Byte[] ReceivedData = new Byte[4 + numberOfPoints * 2]; //创建接收字节数组 int i = 0; //long timespan2 = 0; //System.Diagnostics.Stopwatch watch2 = new System.Diagnostics.Stopwatch(); //watch2.Start(); //开始监视代码运行时间 while (true) { i++; if (sp.BytesToRead == 4 + numberOfPoints * 2) { sp.Read(ReceivedData, 0, sp.BytesToRead); //读取所接收到的数据 byte[] temp = new byte[numberOfPoints]; for (int j = 0; j < numberOfPoints; j++) { try { temp[j] = Convert.ToByte(Convert.ToInt16((Chr(ReceivedData[j * 2 + 1]) + Chr(ReceivedData[j * 2 + 2])), 16)); } catch { return null; } } switch (type) { case "float": value = BytesToFloat(temp).ToString("f4"); break; case "int32": value = BytesToInt32(temp).ToString(); break; case "int16": //value = Status(BytesToInt16(temp)); value = BytesToInt16(temp).ToString(); break; case "byte": value = Convert.ToString(temp[0], 2).PadLeft(8, '0'); break; case "2bytes": value = Convert.ToString(temp[0], 2).PadLeft(8, '0'); break; case "bit": value = Convert.ToString(temp[0], 2).Substring(0, 1); break; } break; } if (i > 100000) break;//10000=440ms左右 } //watch2.Stop(); //停止监视 //timespan2 = watch2.ElapsedMilliseconds; //获取当前实例测量得出的总时间 //return ReceivedData; return value; } catch { return null; } }
最新发布
05-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值