mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Use a better sorting algorithm
This change changes qsort() to use the same code as NetBSD and MacOS because it goes 6x faster than Musl's SmoothSort function. Smoothsort can still be used if you need something that's provenly linearithmic. This change also improves GNU Make performance on whole by 7 percent! netbsd nearly l: 70,196c 22,673ns m: 68,428c 22,102ns musl nearly l: 53,844c 17,391ns m: 58,726c 18,968ns unixv6 nearly l: 65,885c 21,280ns m: 63,082c 20,375ns netbsd reverse l: 120,290c 38,853ns m: 122,619c 39,605ns musl reverse l: 801,826c 258,985ns m: 794,689c 256,680ns unixv6 reverse l: 58,977c 19,049ns m: 59,764c 19,303ns netbsd random l: 146,745c 47,398ns m: 145,782c 47,087ns musl random l: 855,804c 276,420ns m: 850,912c 274,840ns unixv6 random l: 214,325c 69,226ns m: 213,906c 69,090ns netbsd 2n l: 77,299c 24,967ns m: 76,773c 24,797ns musl 2n l: 818,012c 264,213ns m: 818,282c 264,301ns unixv6 2n l: 3,967,009c 1,281,322ns m: 3,941,792c 1,273,177ns https://justine.lol/dox/sort.pdf
This commit is contained in:
parent
55c6297e13
commit
11ec60d5fc
5 changed files with 484 additions and 237 deletions
|
@ -16,13 +16,14 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/mem/alg.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/alg.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/bsr.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
@ -50,11 +51,39 @@ TEST(qsort, test) {
|
|||
}
|
||||
|
||||
BENCH(qsort, bench) {
|
||||
size_t i;
|
||||
size_t n = 1000;
|
||||
long *p1 = gc(malloc(n * sizeof(long)));
|
||||
long *p2 = gc(malloc(n * sizeof(long)));
|
||||
rngset(p1, n * sizeof(long), 0, 0);
|
||||
EZBENCH2("qsort", memcpy(p2, p1, n * sizeof(long)),
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < n; ++i) p1[i] = i + ((lemur64() % 3) - 1);
|
||||
EZBENCH2("qsort nearly", memcpy(p2, p1, n * sizeof(long)),
|
||||
qsort(p2, n, sizeof(long), CompareLong));
|
||||
EZBENCH2("longsort", memcpy(p2, p1, n * sizeof(long)), longsort(p2, n));
|
||||
EZBENCH2("smoothsort nearly", memcpy(p2, p1, n * sizeof(long)),
|
||||
smoothsort(p2, n, sizeof(long), CompareLong));
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < n; ++i) p1[i] = n - i;
|
||||
EZBENCH2("qsort reverse", memcpy(p2, p1, n * sizeof(long)),
|
||||
qsort(p2, n, sizeof(long), CompareLong));
|
||||
EZBENCH2("smoothsort reverse", memcpy(p2, p1, n * sizeof(long)),
|
||||
smoothsort(p2, n, sizeof(long), CompareLong));
|
||||
|
||||
printf("\n");
|
||||
rngset(p1, n * sizeof(long), 0, 0);
|
||||
EZBENCH2("qsort random", memcpy(p2, p1, n * sizeof(long)),
|
||||
qsort(p2, n, sizeof(long), CompareLong));
|
||||
EZBENCH2("smoothsort random", memcpy(p2, p1, n * sizeof(long)),
|
||||
smoothsort(p2, n, sizeof(long), CompareLong));
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < n / 2; ++i) {
|
||||
p1[i] = i;
|
||||
p1[n - i - 1] = i;
|
||||
}
|
||||
EZBENCH2("qsort 2n", memcpy(p2, p1, n * sizeof(long)),
|
||||
qsort(p2, n, sizeof(long), CompareLong));
|
||||
EZBENCH2("smoothsort 2n", memcpy(p2, p1, n * sizeof(long)),
|
||||
smoothsort(p2, n, sizeof(long), CompareLong));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue