-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort_perf_test.c
64 lines (47 loc) · 1.37 KB
/
qsort_perf_test.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include "slist.h"
#include "prng.h"
static bool is_ordered(node_t *list) {
if (!list)
return false;
int value = list->value;
for (node_t *n = list; n; n = n->next) {
if (n->value < value)
return false;
}
return true;
}
static void init_lists(node_t **la, node_t **lb, int cnt) {
prng_set_seed(prng_gen_number());
int val;
while (cnt--) {
val = prng_gen_number();
*la = list_make_node_t(*la, val);
*lb = list_make_node_t(*lb, val);
}
}
int main() {
node_t *lista = NULL;
node_t *listb = NULL;
const int listlen[] = {1 << 5, 1 << 10, 1 << 20};
int size, round = 3;
clock_t beg, end;
for (int i = 0; i < round; i++) {
size = listlen[i];
init_lists(&lista, &listb, size);
printf("Round %d Size = %7d\n", i, size);
beg = clock();
quicksort(&lista);
end = clock();
printf("Recursive:\t%lfs\tPASS: %s\n", (end - beg)/(double)CLOCKS_PER_SEC, is_ordered(lista) ? "TRUE": "FALSE");
beg = clock();
nr_quicksort(&listb);
end = clock();
printf("Non-recursive:\t%lfs\tPASS: %s\n", (end - beg)/(double)CLOCKS_PER_SEC, is_ordered(listb) ? "TRUE": "FALSE");
list_free(&lista);
list_free(&listb);
}
return 0;
}