Delphi high precision timing method

xiaoxiao2021-03-06  60

// Take millive time accuracy (Method 1): VAR T1, T2: INT64; R1: INT64; Begin T1: = gettickcount; // Get start counting Windows API Sleep (1000); {DO ...} // Time to schedule the code T2: = getTickCount; // Get the end count value R1: = T2-T1; // get the timing time, unit milliseconds (MS) ShowMessage (INTTOSTR (R1));

// Take millive times time accuracy (Method 2): // use dateutils; // Reference Dateutils unit VAR T1, T2: TDATETIME; R1: INT64; Begin T1: = now (); // Get Start Timing Time SLEEP (1000 ); {DO ...} // Execute the code T2: = now (); // Get the end timing R1: = SecondsbetWeen (T2, T1); // get the timing time, the unit seconds (s) R1 : = MilliseCondsbetWeen (T2, T1); // Get the timing time, unit milliseconds (MS) ShowMessage (INTTOSTR (R1));

// Note: The above two ways seem to have only 0.01 seconds of timing accuracy.

// Take the system level time accuracy: var C1: int64; T1, T2: INT64; R1: Double; begin queryperformancefrequency (c1); // windows API returns the count frequency (Intel86: 1193180) (Getting the system's high performance frequency counter The number of vibrations within one milliccity) QueryPerformanceCounter (T1); // Windows API Get start counting value SLEEP (1000); {DO ...} // Execute the code QueryperFormanceCounter to be timed; // Get the end count value R1 : = (T2-T1) / C1; // acquire timing time, unit seconds (s) R1: = (T2-T1) / c1 * 1000; // obtain timing time, unit millisecond (MS) R1: = (T2 -t1) / c1 * 1000000; // get the timing time, unit microsecond showMessage (FLOATTOSTR (R1)); END;

转载请注明原文地址:https://www.9cbs.com/read-84314.html

New Post(0)