.NET realizes nanosecond level calculation
1) Establish a VC.NET managed class library
Using Namespace System;
Namespace mltimerdot
{
/ / Get the computer started to the current clock cycle
Unsigned __int64 getcyclecount (void)
{
_asm _emit 0x0f
_asm _emit 0x31
}
// Declaration .NET class
Public __gc class mltimer
{
protected:
UINT64 m_Startcycle;
UINT64 M_OVERHEAD;
PUBLIC:
Mltimer (void)
{
/ / In order to calculate a clock cycle that calls a getcyclecount ()
m_overhead = 0;
START ();
m_overhead = stop ();
}
// Calculation stop
UINT64 STOP (VOID)
{
Return getCycleCount () - m_startcycle-m_overhead;
}
// Start
Void Start (void)
{
m_startcycle = getCyClecount ();
}
__property virtual uint64 get_overhead ()
{
Return M_Overhead;
}
}
}
2) Test code
// c # Release a button test after reference
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); // With this MHz that can calculate CPU
Timer.Start (); // Start
// Test code (test declare a DataTable time)
DataTable TD = New DataTable ();
UINT64 TIME1 = Timer.Stop (); // Stop
String s = string.format ("CPU {0}. {1} MHz / N declaration MLTIMER class system overhead {2: n} clock cycle / N This operating system overhead {3: N} clock cycle / n { 4: n} ns ",
Cpuspeed10 / 10, CPuspeed10% 10, Timer.overhead,
Time1,
Time1 * 1000 / cpuspeed10);
Messagebox.show (s);
}
/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------
The MLTIMER class library must use VC. NET development, only VC. NET can fall into assembly (cool).
Compilation completion can be used in any .NET language (of course, can also be encapsulated into API, or COM components).
Think about it can be displayed on the web later:
You used this query: xxxxx nanosecond.
Is it very cool ^ _ ^, of course, some precision calculations can be made.
This is the next article in 9CBS for the second time. I hope everyone can like it.
Demonstration code:
http://www.kairuisoft.com/uploads/mltimerdot.rar
Disclaimer: This source code you can be used for any use, but please inform me when used for business programs, thank you. QQ 65423574;