Cross-platform C ++ millisecond timing -- Taking the Quick Sort Algorithm as an example, quicksort
# Include <iostream> # include <stdlib. h> using namespace std; void Qsort (int a [], int low, int high) {if (low> = high) return; int first = low; int last = high; int key = a [first]; while (first <last) {while (first <last & a [last]> = key) -- last; a [first] = a [last]; while (first <last & a [first] <= key) ++ first; a [last] = a [first];} a [first] = key; Qsort (a, low, first-1); Qsort (a, first + 1, high);} int main (int argc, char ** argv) {// determine the number of command line parameters if (argc = 0) {cout <"Usage: quicksort N \ n"; return 0 ;} // The 2nd parameters in the command line are converted to a long integer long N = atoi (argv [1]); // randomization number srand (time (0 )); // dynamically allocate memory int * a; a = new int [N]; // C ++ for (long I = 0; I <N; I ++) a [I] = rand (); // timing clock_t t1 = clock (); Qsort (a, 0, N-1 ); /* The third parameter in the original text must be reduced by 1; otherwise, memory leakage */clock_t t2 = clock (); cout <(double) (t2-t1) /CLOCKS_PER_SEC <"millisecond" <endl; // output for (int I = 0; I <sizeof (a)/sizeof (a [0]); I ++) {cout <a [I] <"" ;}* // release the applied memory delete [] a; // C ++ return 0 ;}