Skip to content

Instantly share code, notes, and snippets.

@vmxdev
Last active October 24, 2019 15:29
Show Gist options
  • Select an option

  • Save vmxdev/f095b667a85df5c55b74e5ec5e800597 to your computer and use it in GitHub Desktop.

Select an option

Save vmxdev/f095b667a85df5c55b74e5ec5e800597 to your computer and use it in GitHub Desktop.
/*
* $ cc -O3 -Wall -pedantic -Wextra qtst.c -o qtst
* $ time ./qtst
real 0m39,543s
user 0m37,288s
sys 0m0,732s
*/
#include <stdio.h>
#include <float.h>
#include <stdlib.h>
#define N 50000000
#define DMIN DBL_MAX
#define DMAX DBL_MIN
static int
cmpfunc(const void *x, const void *y)
{
double xx = *(double*)x, yy = *(double*)y;
if (xx < yy) return -1;
if (xx > yy) return 1;
return 0;
}
int
main()
{
size_t i;
double *arr;
arr = malloc(N * sizeof(double));
if (!arr) {
fprintf(stderr, "Can't allocate memory\n");
return EXIT_FAILURE;
}
for (i=0; i<N; i++) {
double f = (double)rand() / RAND_MAX;
/* DMIN + f * (DMAX - DMIN) */
arr[i] = DMIN + f * DMAX - f * DMIN;
}
for (i=0; i<10; i++) {
qsort(arr, N, sizeof(double), cmpfunc);
}
free(arr);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment