From bff972ea91f40482c2abb28d9ad1d1a6fe285615 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 26 Apr 2020 18:10:20 -0700 Subject: sys/libn/sort.c --- Makefile | 2 +- include/libn.h | 7 +++++- sys/libn/test.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index cb9951c..c7a94e1 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ LIB_DIR := lib OBJ_DIR := build # Flags, Libraries and Includes -CFLAGS := -g -fno-strict-aliasing -fwrapv -fms-extensions -Wno-microsoft-anon-tag -Wno-incompatible-function-pointer-types +CFLAGS := -g -O2 -fno-strict-aliasing -fwrapv -fms-extensions -Wno-microsoft-anon-tag -Wno-incompatible-function-pointer-types AFLAGS := -f elf64 INCS := -I$(INC_DIR) ELIBS := diff --git a/include/libn.h b/include/libn.h index b994159..4b6d702 100644 --- a/include/libn.h +++ b/include/libn.h @@ -286,8 +286,13 @@ error gz·flush(gz·Stream *s); vlong gz·seek(gz·Stream *s, long off, enum SeekPos whence); // ----------------------------------------------------------------------------- -// Error handling functions. +// error handling functions void errorf(const byte* fmt, ...); #define panicf(...) (errorf(__VA_ARGS__), assert(0)) + +// ----------------------------------------------------------------------------- +// sorting + +void sort·ints(uintptr n, int arr[]); diff --git a/sys/libn/test.c b/sys/libn/test.c index 3f97453..5ee230f 100644 --- a/sys/libn/test.c +++ b/sys/libn/test.c @@ -1,6 +1,8 @@ #include #include +#include + uintptr printtest(Coro *c, uintptr d) { @@ -51,8 +53,8 @@ filter(Coro *c, uintptr data) return 0; } -int -main() +error +test·coro() { int i; Coro *c[4]; @@ -103,4 +105,65 @@ main() num = coro·yield(cur, (uintptr)&msg); printf("--> prime number %lu\n", num); } + return 0; +} + +int +less(void* a, void* b) +{ + int ai, bi; + ai = *(int*)a; + bi = *(int*)b; + + return ai - bi; +} + +error +test·sort() +{ + clock_t t; + int i, test[1000000]; + for (i = 0; i < arrlen(test); i++) { + test[i] = rand(); + } + + t = clock(); + sort·ints(arrlen(test), test); + t = clock() - t; + printf("inlined code took %f ms to execute\n", 1000.*t/CLOCKS_PER_SEC); + + for (i = 0; i < arrlen(test); i++) { + test[i] = rand(); + } + + t = clock(); + qsort(test, arrlen(test), sizeof(int), &less); + t = clock() - t; + printf("std qsort code took %f ms to execute\n", 1000.*t/CLOCKS_PER_SEC); + + /* + for (i = 1; i < arrlen(test); i++) { + if (test[i] >= test[i-1]) { + printf("%d is less that %d\n", test[i], test[i-1]); + } else { + printf("ERROR: %d is NOT less that %d\n", test[i], test[i-1]); + } + } + */ + + return 0; +} + +error +main() +{ + error err; +#if 0 + if (err = test·coro(), err) { + errorf("test fail: coroutine"); + } +#endif + if (err = test·sort(), err) { + errorf("test fail: coroutine"); + } } -- cgit v1.2.1