Net 实现纳秒级别计算

1)建立VC.NET 托管类库 using namespace System; namespace MLTimerDot { //得到计算机启动到现在的时钟周期 unsigned __int64 GetCycleCount(void) { _asm _emit 0x0F _asm _emit 0x31 } //声明 .NET 类 public __gc class MLTimer { protected: UInt64 m_startcycle; UInt64 m_overhead; public: MLTimer(void) { //为了计算更精确取得调用一个 GetCycleCount() 的时钟周期 m_overhead=0; Start(); m_overhead=Stop(); } //计算停止 UInt64 Stop(void) { return GetCycleCount()-m_startcycle-m_overhead; } //计算开始 void Start(void) { m_startcycle=GetCycleCount(); } __property virtual UInt64 get_Overhead() { return m_overhead; } }; } 2)测试代码 //C# 引用后放一个Button 测试 private void button1_Click(object sender, System.EventArgs e) { MLTimerDot.MLTimer timer=new MLTimerDot.MLTimer(); timer.Start(); Thread.Sleep(1000); UInt64 cpuspeed10=(ulong)(timer.Stop()/100000); //通过这个可以算出 CPU 的mhz timer.Start();//开始 //测试代码(测试声明一个DataTable 用的时间) DataTable td= new DataTable(); UInt64 time1=timer.Stop();//停止 String s= String.Format("CPU {0}.{1} mhz\n声明 MLTimer 类的系统开销 {2:n} 时钟周期\n本操作系统开销 {3:n} 个时钟周期\n使用 {4:n} ns", cpuspeed10/10,cpuspeed10%10,timer.Overhead, time1, time1*10000/cpuspeed10); MessageBox.Show(s); } /*-------------------------------------------------------------------------------------------

posted on   mikelij  阅读(509)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2009年3月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示