aboutsummaryrefslogtreecommitdiff
path: root/sort-benchmark/integers/sort.c
blob: 2a23d9d7b7db7b0760adcb5188c454930b765b11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define _POSIX_C_SOURCE 200809L /* Required to use clock_gettime */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int a[ARRAYSIZE];

int compar(const void *x, const void *y) { return *(int *)x - *(int *)y; }

int main() {
	srand(time(NULL));
	for (int i = 0; i < ARRAYSIZE; i++) a[i] = rand() % 1000000000;

	struct timespec begin;
	clock_gettime(CLOCK_MONOTONIC, &begin);

	qsort(a, ARRAYSIZE, sizeof(int), compar);

	struct timespec end;
	clock_gettime(CLOCK_MONOTONIC, &end);
	double time = end.tv_sec - begin.tv_sec +
		(end.tv_nsec - begin.tv_nsec) / 1000000000.0;

	printf("(%d) C time for %d numbers: %lfs\n",
		a[ARRAYSIZE/2], ARRAYSIZE, time);

	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net