I sorted out a small tool class from Lao Zhao, a small tool class for testing, and a tool class from Lao Zhao.

Source: Internet
Author: User

I sorted out a small tool class from Lao Zhao, a small tool class for testing, and a tool class from Lao Zhao.

I sorted out a test tool class from Lao Zhao without changing anything else. I just changed the class name and method name to a name that I prefer.

The Code is as follows:

/// <Summary>
/// Code performance tester
/// </Summary>
Public static class CodePerformanceTester
{
/// <Summary>
/// The initialization tool first sets the priority of the current process and the current thread to the highest, which can reduce the interference caused by the operating system scheduling.
/// Then call the Time method for "push", let JIT compile the IL code at cost, and let the Time method "Enter the state" as soon as possible ".
/// This method should be executed before the test.
/// </Summary>
Public static void Initialize ()
{
Process. GetCurrentProcess (). PriorityClass = ProcessPriorityClass. High;
Thread. CurrentThread. Priority = ThreadPriority. Highest;
Test ("", 1, () => {});
}

/// <Summary>
/// Core method of performance testing: printing the time consumed, the CPU clock cycle consumed, and the number of garbage collection times
/// </Summary>
/// <Param name = "name"> name of the test instance, which is easy to remember </param>
/// <Param name = "iteration"> number of iterations </param>
/// <Param name = "action"> Method for Testing </param>
Public static void Test (string name, int iteration, Action action)
{
If (String. IsNullOrEmpty (name) return;

// 1. Retain the foreground color of the current console and use the yellow output name parameter.
Lelecolor currentForeColor = Console. ForegroundColor;
Console. ForegroundColor = ConsoleColor. Yellow;
Console. WriteLine (name );

// 2. Force GC to collect and record the number of times each generation has collected.
GC. Collect (GC. MaxGeneration, GCCollectionMode. Forced );
Int [] gcCounts = new int [GC. MaxGeneration + 1];
For (int I = 0; I <= GC. MaxGeneration; I ++)
{
GcCounts [I] = GC. CollectionCount (I );
}

// 3. Execute the code and record the consumed time and CPU clock cycle.
Stopwatch watch = new Stopwatch ();
Watch. Start ();
Ulong cycleCount = GetCycleCount ();
For (int I = 0; I <iteration; I ++) action ();
Ulong cpuCycles = GetCycleCount ()-cycleCount;
Watch. Stop ();

// 4. Restore the default foreground color of the console and print the consumption time and CPU clock cycle.
Console. ForegroundColor = currentForeColor;
Console. WriteLine ("\ tTime Elapsed: \ t" + watch. ElapsedMilliseconds. ToString ("N0") + "ms ");
Console. WriteLine ("\ tCPU Cycles: \ t" + cpuCycles. ToString ("N0 "));

// 5. print the number of garbage collection times during execution.
For (int I = 0; I <= GC. MaxGeneration; I ++)
{
Int count = GC. CollectionCount (I)-gcCounts [I];
Console. WriteLine ("\ tGen" + I + ": \ t" + count );
}

Console. WriteLine ();
}

/// <Summary>
/// Obtain the number of cycles
/// </Summary>
/// <Returns> Number of cycles of the returned thread </returns>
Private static ulong GetCycleCount ()
{
Ulong cycleCount = 0;
QueryThreadCycleTime (GetCurrentThread (), ref cycleCount );
Return cycleCount;
}

/// <Summary>
/// Obtain the thread cycle time
/// </Summary>
/// <Param name = "threadHandle"> thread handle </param>
/// <Param name = "cycleTime"> return cycle time </param>
/// <Returns> return a Boolean value. true indicates that the thread cycle is successfully obtained. false indicates that the thread cycle time fails to be obtained. </returns>
[DllImport ("kernel32.dll")]
[Return: financialas (UnmanagedType. Bool)]
Static extern bool QueryThreadCycleTime (IntPtr threadHandle, ref ulong cycleTime );

/// <Summary>
/// Obtain the reference handle of the current thread
/// </Summary>
/// <Returns> return the reference handle of the current thread </returns>
[DllImport ("kernel32.dll")]
Static extern IntPtr GetCurrentThread ();
}

The above is the complete code, which can be copied and recorded to prevent forgetting.

Related Article

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.