]>
git.saurik.com Git - apple/libc.git/blob - tests/psort.c
4 #include <mach/clock_types.h>
5 #include <TargetConditionals.h>
7 #include <darwintest.h>
9 typedef unsigned long T
;
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;
17 T_DECL(psort
, "psort(3)")
19 struct timeval tv_start
, tv_stop
;
20 struct rusage ru_start
, ru_stop
;
21 uint64_t pwt
, put
, qwt
, qut
;
25 const size_t nel
= 2048000;
27 const size_t nel
= 20480000;
29 const size_t width
= sizeof(T
), bufsiz
= nel
* width
;
32 arc4random_buf(buf
, bufsiz
);
33 sorted
= malloc(bufsiz
);
34 memcpy(sorted
, buf
, bufsiz
);
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
);
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
);
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
);
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
);
60 for (size_t i
= 0; i
< nel
; i
++) {
61 if (buf
[i
] != sorted
[i
]) {
62 T_ASSERT_EQ(buf
[i
], sorted
[i
], NULL
);
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");