Delphi high-precision timing method (transfer)

xiaoxiao2021-03-06  52

// Take millive time accuracy (Method 1): VAR T1, T2: INT64; R1: INT64; Begin T1: = gettickcount; // Get start counting Windows API Sleep (1000); {DO ...} // Time code T2: = gettickcount; // Get end count value R1: = T2-T1; // get the timing time, unit milliseconds (MS) ShowMessage (INTTOSTR (R1)); end; // Take millisecond time accuracy (Method 2): // Use Dateutils; // Reference Dateutils Unit Var T1, T2: TDATETIME; R1: INT64; Begin T1: = NOW (); // Get Start Time Sleep (1000); {DO ... } // Execute the code T2: = now (); // Get the end timing time 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)); end; // Note: The above two methods seem to generate only 0.01 second time accuracy // Take system level time accuracy : VAR C1: INT64; T1, T2: INT64; R1: Double; Begin QueryperFormanceFrequency (C1); // Windows API Returns the counting frequency (Intel86: 1193180) (Get the system's high performance frequency counter in one milliseconds) 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 second (s) R1: = (T2-T1) / c1 * 1000; // get the 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-83970.html

New Post(0)