From 5bf3a812c15b7ff39395af78f30e09b962c7ac3c Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 22 Jul 2017 00:34:00 +0200 Subject: [PATCH] Add benchmark for sorting function. --- bench/Makefile | 1 + bench/bench-collectionc.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/bench/Makefile b/bench/Makefile index 8b9321e7..70808812 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -109,6 +109,7 @@ bench-collectionc0: @./bench-collectionc.exe 30 @./bench-collectionc.exe 40 @./bench-collectionc.exe 41 + @./bench-collectionc.exe 50 bench-plain: $(CC) $(CFLAGS) $(CPPFLAGS) bench-plain-c.c -o bench-plain-c.exe diff --git a/bench/bench-collectionc.c b/bench/bench-collectionc.c index 2da37d37..4bd2aea0 100644 --- a/bench/bench-collectionc.c +++ b/bench/bench-collectionc.c @@ -216,6 +216,35 @@ test_dict_big(unsigned long n) /********************************************************************************************/ +static int cmp_float(const void * a, const void *b) +{ + const float *const*pa = a, *const*pb = b; + return (**pa < **pb) ? -1 : (**pa > **pb); +} + +static void test_sort(size_t n) +{ + Array *a1; + enum cc_stat stat; + + stat = array_new(&a1); + if (stat != CC_OK) abort(); + for(size_t i = 0; i < n; i++) { + float *v = malloc(sizeof(float)); + *v = rand_get(); + stat = array_add(a1, v ); + if (stat != CC_OK) abort(); + } + array_sort(a1, cmp_float); + void *e; + stat = array_get_at(a1, 0, &e); + if (stat != CC_OK) abort(); + g_result = *(float*)e; + array_destroy(a1); +} + +/********************************************************************************************/ + int main(int argc, const char *argv[]) { int n = (argc > 1) ? atoi(argv[1]) : 0; @@ -229,6 +258,7 @@ int main(int argc, const char *argv[]) test_function("Dict time", 1000000, test_dict); if (n == 41) test_function("DictB time", 1000000, test_dict_big); + if (n == 50) + test_function("Sort time", 10000000, test_sort); exit(0); } -