#include #include #include #include #include #include #include #include namespace chrono = std::chrono; // Custom function to read command line arguments std::pair read_arguments(int argc, char *argv[]); int compare_ip(void const *a, void const *b) { return *(int const *)a - *(int const *)b; } /** * Read number of elements (N) from the command line, generate a * random vector of this size and sort it. * Repeat M times for statistics (M is the second command line argument). */ int main(int argc, char *argv[]) { // Read N and M from command line auto [N, M] = read_arguments(argc, argv); // Function for random number generation std::random_device entropy; std::mt19937 gen(entropy()); // Randomness generator. std::uniform_int_distribution<> dis(0, 1000*N); auto draw = [&gen, &dis] () { return dis(gen); }; int *rnd_array = new int[N]; // Time sorting M random arrays of N elements. double elapsed = 0; for (int j = 0; j < M; j++) { std::generate(rnd_array, rnd_array + N, draw); auto t1 = chrono::high_resolution_clock::now(); qsort(rnd_array, N, sizeof(int), compare_ip); auto t2 = chrono::high_resolution_clock::now(); auto dt = chrono::duration_cast(t2 - t1); elapsed += dt.count(); } // Show timing resuts. std::cout << N << " " << elapsed/M/1e6 << std::endl; } std::pair read_arguments(int argc, char *argv[]) { int N, M; // We need exactly three arguments (considering program name). if (argc != 3) { std::cout << "Usage: " << argv[0] << " \n"; exit(1); } // Read arguments. try { N = std::stoi(argv[1]); M = std::stoi(argv[2]); } catch (std::invalid_argument &) { std::cerr << "Command line argument invalid: must be int.\n"; exit(1); } catch (std::out_of_range &) { std::cerr << "Number too large or too small.\n"; exit(1); } return {N, M}; }