Net to achieve Nanosecond-level computing

Source: Internet
Author: User
Tags emit
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 /*-------------------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.