Using System; Using System. Web; Using System. Collections. Generic; Using System. Text; Using System. Diagnostics; Using System. Threading; Using System. Runtime. InteropServices; Namespace MYORM. COMMON { Public interface IAction { Void Action (); } Public static class CodeTimer { [DllImport ("kernel32.dll", SetLastError = true)] Static extern bool GetThreadTimes (IntPtr hThread, out long lpCreationTime, Out long lpExitTime, out long lpKernelTime, out long lpUserTime ); [DllImport ("kernel32.dll")] Static extern IntPtr GetCurrentThread (); Public delegate void ActionDelegate (); Private static long GetCurrentThreadTimes () { Long l; Long kernelTime, userTimer; GetThreadTimes (GetCurrentThread (), out l, out l, out kernelTime, Out userTimer ); Return kernelTime + userTimer; } Static CodeTimer () { Process. GetCurrentProcess (). PriorityClass = ProcessPriorityClass. High; Thread. CurrentThread. Priority = ThreadPriority. Highest; } Public static void Time (string name, int iteration, ActionDelegate action) { If (String. IsNullOrEmpty (name )) { Return; } If (action = null) { Return; } // 1. Print name // Lelecolor currentForeColor = Console. ForegroundColor; // Console. ForegroundColor = ConsoleColor. Yellow; // Console. WriteLine (name ); HttpContext. Current. Response. Write ("<font color =" blue ">" + name + "</font> <br/> "); // 2. Record the latest GC counts // GC. Collect (GC. MaxGeneration, GCCollectionMode. Forced ); GC. Collect (GC. MaxGeneration ); Int [] gcCounts = new int [GC. MaxGeneration + 1]; For (int I = 0; I <= GC. MaxGeneration; I ++) { GcCounts [I] = GC. CollectionCount (I ); } // 3. Run action Stopwatch watch = new Stopwatch (); Watch. Start (); Long ticksFst = GetCurrentThreadTimes (); // 100 nanosecond one tick For (int I = 0; I <iteration; I ++) action (); Long ticks = GetCurrentThreadTimes ()-ticksFst; Watch. Stop (); // 4. Print CPU // Console. ForegroundColor = currentForeColor; HttpContext. Current. Response. Write ("all time consumed: tt" + watch. ElapsedMilliseconds. ToString ("N0") + "ms <br/> "); HttpContext. Current. Response. Write ("execution time:" + (watch. ElapsedMilliseconds/iteration). ToString ("N0") + "ms <br/> "); // HttpContext. Current. Response. Write ("CPU time: tt" + (ticks * 100). ToString ("N0") + "ns <br/> "); // HttpContext. current. response. write ("CPU time (one time): t" + (ticks * 100/iteration ). toString ("N0") + "ns <br/> "); // 5. Print GC // For (int I = 0; I <= GC. MaxGeneration; I ++) //{ // Int count = GC. CollectionCount (I)-gcCounts [I]; // HttpContext. Current. Response. Write ("tGen" + I + ": ttt" + count + "<br/> "); //} HttpContext. Current. Response. Write ("<br/> "); } Public static void Time (string name, int iteration, IAction action) { If (String. IsNullOrEmpty (name )) { Return; } If (action = null) { Return; } // 1. Print name // Lelecolor currentForeColor = Console. ForegroundColor; // Console. ForegroundColor = ConsoleColor. Yellow; HttpContext. Current. Response. Write (name + "<br/> "); // 2. Record the latest GC counts // GC. Collect (GC. MaxGeneration, GCCollectionMode. Forced ); GC. Collect (GC. MaxGeneration ); Int [] gcCounts = new int [GC. MaxGeneration + 1]; For (int I = 0; I <= GC. MaxGeneration; I ++) { GcCounts [I] = GC. CollectionCount (I ); }
// 3. Run action Stopwatch watch = new Stopwatch (); Watch. Start (); Long ticksFst = GetCurrentThreadTimes (); // 100 nanosecond one tick For (int I = 0; I <iteration; I ++) action. Action (); Long ticks = GetCurrentThreadTimes ()-ticksFst; Watch. Stop (); // 4. Print CPU // Console. ForegroundColor = currentForeColor; HttpContext. Current. Response. Write ("tTime Elapsed: tt" + Watch. ElapsedMilliseconds. ToString ("N0") + "ms <br/> "); HttpContext. Current. Response. Write ("tTime Elapsed (one time):" + (Watch. ElapsedMilliseconds/iteration). ToString ("N0") + "ms <br/> "); HttpContext. Current. Response. Write ("tCPU time: tt" + (ticks * 100). ToString ("N0") + "ns <br/> "); Console. WriteLine ("tCPU time (one time): t" + (ticks * 100/ Iteration). ToString ("N0") + "ns "); // 5. Print GC For (int I = 0; I <= GC. MaxGeneration; I ++) { Int count = GC. CollectionCount (I)-gcCounts [I]; HttpContext. Current. Response. Write ("tGen" + I + ": ttt" + count + "<br/> "); } } } } |