Add treetable for CollectionsC
This commit is contained in:
1 parent
cc4fea3d68
commit
cb79fd9d41
2 files changed
+38
No files matched your search
@@ -106,6 +106,7 @@ bench-collectionc0:
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -I "$${COLLECTIONC}/include" -L "$${COLLECTIONC}" -static bench-collectionc.c -lcollectc -o bench-collectionc.exe
|
||||
@./bench-collectionc.exe 10
|
||||
@./bench-collectionc.exe 20
|
||||
@./bench-collectionc.exe 30
|
||||
|
||||
bench-plain:
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) bench-plain-c.c -o bench-plain-c.exe
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "array.h"
|
||||
#include "slist.h"
|
||||
#include "treetable.h"
|
||||
|
||||
#include "common.h"
|
||||
|
||||
@@ -79,6 +80,40 @@ static void test_list(size_t n)
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
static int compare(const void *a, const void *b)
|
||||
{
|
||||
const uintptr_t pa = (uintptr_t) a;
|
||||
const uintptr_t pb = (uintptr_t) b;
|
||||
return (pa < pb) ? -1 : (pa > pb);
|
||||
}
|
||||
|
||||
static void test_rbtree(size_t n)
|
||||
{
|
||||
TreeTable *tree;
|
||||
enum cc_stat stat;
|
||||
|
||||
stat = treetable_new(compare, &tree);
|
||||
if (stat != CC_OK) abort();
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
void *key = (void*)(uintptr_t) i;
|
||||
stat = treetable_add(tree, key, key);
|
||||
if (stat != CC_OK) abort();
|
||||
}
|
||||
rand_init();
|
||||
unsigned int s = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
void *e;
|
||||
void *key = (void*) (uintptr_t) rand_get();
|
||||
stat = treetable_get(tree, key, &e);
|
||||
if (stat == CC_OK)
|
||||
s += (uintptr_t)e;
|
||||
}
|
||||
g_result = s;
|
||||
treetable_destroy(tree);
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
int n = (argc > 1) ? atoi(argv[1]) : 0;
|
||||
@@ -86,6 +121,8 @@ int main(int argc, const char *argv[])
|
||||
test_function("List time",10000000, test_list);
|
||||
if (n == 20)
|
||||
test_function("Array time", 100000000, test_array);
|
||||
if (n == 30)
|
||||
test_function("Rbtree time", 1000000, test_rbtree);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user