]> git.saurik.com Git - apple/libc.git/blob - tests/psort.c
Libc-1244.1.7.tar.gz
[apple/libc.git] / tests / psort.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <mach/clock_types.h>
5 #include <TargetConditionals.h>
6
7 #include <darwintest.h>
8
9 typedef unsigned long T;
10
11 static int
12 comparT(const void* a, const void* b) {
13 const T x = *(T*)a, y = *(T*)b;
14 return x < y ? -1 : x > y ? 1 : 0;
15 }
16
17 T_DECL(psort, "psort(3)")
18 {
19 struct timeval tv_start, tv_stop;
20 struct rusage ru_start, ru_stop;
21 uint64_t pwt, put, qwt, qut;
22
23 T *buf, *sorted;
24 #if TARGET_OS_BRIDGE
25 const size_t nel = 2048000;
26 #else
27 const size_t nel = 20480000;
28 #endif
29 const size_t width = sizeof(T), bufsiz = nel * width;
30
31 buf = malloc(bufsiz);
32 arc4random_buf(buf, bufsiz);
33 sorted = malloc(bufsiz);
34 memcpy(sorted, buf, bufsiz);
35
36 getrusage(RUSAGE_SELF, &ru_start);
37 gettimeofday(&tv_start, NULL);
38 psort(sorted, nel, width, comparT);
39 gettimeofday(&tv_stop, NULL);
40 getrusage(RUSAGE_SELF, &ru_stop);
41
42 pwt = ((uint64_t)tv_stop.tv_sec * USEC_PER_SEC + tv_stop.tv_usec) -
43 ((uint64_t)tv_start.tv_sec * USEC_PER_SEC + tv_start.tv_usec);
44 put = ((uint64_t)ru_stop.ru_utime.tv_sec * USEC_PER_SEC + ru_stop.ru_utime.tv_usec) -
45 ((uint64_t)ru_start.ru_utime.tv_sec * USEC_PER_SEC + ru_start.ru_utime.tv_usec);
46 T_LOG("psort: wall-time=%llu us; user-time=%llu us", pwt, put);
47
48 getrusage(RUSAGE_SELF, &ru_start);
49 gettimeofday(&tv_start, NULL);
50 qsort(buf, nel, width, comparT);
51 gettimeofday(&tv_stop, NULL);
52 getrusage(RUSAGE_SELF, &ru_stop);
53
54 qwt = ((uint64_t)tv_stop.tv_sec * USEC_PER_SEC + tv_stop.tv_usec) -
55 ((uint64_t)tv_start.tv_sec * USEC_PER_SEC + tv_start.tv_usec);
56 qut = ((uint64_t)ru_stop.ru_utime.tv_sec * USEC_PER_SEC + ru_stop.ru_utime.tv_usec) -
57 ((uint64_t)ru_start.ru_utime.tv_sec * USEC_PER_SEC + ru_start.ru_utime.tv_usec);
58 T_LOG("qsort: wall-time=%llu us; user-time=%llu us", qwt, qut);
59
60 for (size_t i = 0; i < nel; i++) {
61 if (buf[i] != sorted[i]) {
62 T_ASSERT_EQ(buf[i], sorted[i], NULL);
63 }
64 }
65
66 free(sorted);
67 free(buf);
68
69 T_EXPECT_LE((double)pwt/qwt, 1.2, "psort/qsort wall time");
70 T_EXPECT_LE((double)qut/put, 1.2, "qsort/psort user time");
71 }