1) create a VC. NET hosted class library
Using namespace System;
Namespace MLTimerDot
{
// Obtain the clock cycle from the computer startup to the present
Unsigned _ int64 GetCycleCount (void)
{
_ Asm _ emit 0x0F
_ Asm _ emit 0x31
}
// Declare the. NET class
Public _ gc class MLTimer
{
Protected:
UInt64 m_startcycle;
UInt64 m_overhead;
Public:
MLTimer (void)
{
// To calculate the more precise time period for calling a GetCycleCount () call
M_overhead = 0;
Start ();
M_overhead = Stop ();
}
// Computing stops
UInt64 Stop (void)
{
Return GetCycleCount ()-m_startcycle-m_overhead;
}
// Start computing
Void Start (void)
{
M_startcycle = GetCycleCount ();
}
_ Property virtual UInt64 get_Overhead ()
{
Return m_overhead;
}
};
}
2) test code
// C # put a Button after reference for testing
Private void button#click (object sender, System. EventArgs e)
{
MLTimerDot. MLTimer timer = new MLTimerDot. MLTimer ();
Timer. Start ();
Thread. Sleep (1000 );
UInt64 cpuspeed10 = (ulong) (timer. Stop ()/100000); // you can use this to calculate the CPU mhz.
Timer. Start (); // Start
// Test code (time used to declare a DataTable)
DataTable td = new DataTable ();
UInt64 time1 = timer. Stop (); // Stop
String s = String. format ("CPU {0 }. {1} mhz \ n declares the MLTimer class system overhead {2: n} clock cycle \ n the operating system overhead {3: n} clock cycles \ n use {4: n} ns ",
Cpuspeed10/10, cpuspeed10 % 10, timer. Overhead,
Time1,
Time 1*10000/cpuspeed10 );
MessageBox. Show (s );
}
/* Configure /*-------------------------------------------------------------------------------------------