/* * TDRB Tree benchmarks */ #include #include #include #include #include #include "timer.h" #include "tdrb.h" #include "mt.h" #include "driver.h" #define COUNT 10000000 #define MAX_RANGE 10000000 void test_insert(Node* root) { // Create a timer Timer t; timer_init(&t); // Generate COUNT random numbers timer_init(&t); // Generate COUNT random numbers uint32_t* random = malloc(COUNT * sizeof(uint32_t)); Node* nodes = malloc(COUNT * sizeof(Node)); shuffle_random(random, nodes, COUNT, MAX_RANGE); int i; // prime the tree with 1 Mio random numbers for (i = 0; i < COUNT/2; i++) { insert(random[i], nodes[i], root); } // Start the timer timer_start(&t); // Insert an additional 1 Mio random numbers into the tree for (i = COUNT/2; i < COUNT; i++) { insert(random[i], nodes[i], root); } // Stop the timer and print out some statistics timer_end(&t); printf("Inserted %d elements (%d unique):\n", COUNT/2, treeSize(*root)); display_statistics(&t); } void test_find(Node* root) { // Create a timer Timer t; timer_init(&t); // Generate COUNT random numbers uint32_t* random = malloc(COUNT/2 * sizeof(uint32_t)); generate_random(random, COUNT/2, MAX_RANGE); // Start the timer timer_start(&t); // Get COUNT random numbers into the tree int i; for (i = 0; i < COUNT/2; get(random[i++], *root)); // Stop the timer and print out some statistics timer_end(&t); printf("Retrieved %d elements:\n", COUNT/2); display_statistics(&t); } /* void test_remove(Node* root) { // Create a timer Timer t; timer_init(&t); // Generate COUNT random numbers uint32_t* random = malloc(COUNT * sizeof(uint32_t)); shuffle_random_no_nodes(random, COUNT, MAX_RANGE); // Start the timer timer_start(&t); // Remove COUNT random numbers into the tree int i; for (i = 0; i < COUNT/2; removeNode(random[i++], root)); // Stop the timer and print out some statistics timer_end(&t); printf("Removed %d elements (%d remaining):\n", COUNT/2, treeSize(*root)); display_statistics(&t); } */ int main(void) { Node root = NULL; test_insert(&root); printf("Height of tree: %d\n", height(root)); test_find(&root); // test_remove(&root); // printf("Height of tree: %d\n", height(root)); return EXIT_SUCCESS; } // Populates array with count number of rand() generated pseudorandom // numbers up to maxRange void generate_random(uint32_t* array, uint32_t count, uint32_t maxRange) { int i; // Set the seed mt_seed(time(NULL) * mt_rand(maxRange)); // Populate the array for (i = 0; i < count; array[i++] = (mt_rand(maxRange))); } // Populates array with count numbers and then shuffles them void shuffle_random(uint32_t* array, Node* nodes, uint32_t count, uint32_t maxRange) { int i; // Set the seed mt_seed(time(NULL) * mt_rand(maxRange)); // Populate the array for (i = 0; i < count; array[i++] = i); // Shuffle the elements int i1, i2; int temp; for (i = 0; i < count; i++) { i1 = (mt_rand(maxRange)); i2 = (mt_rand(maxRange)); temp = array[i1]; array[i1] = array[i2]; array[i2] = temp; } for (i = 0; i < count; i++) nodes[i] = makeNode(array[i]); } // Populates array with count numbers and then shuffles them void shuffle_random_no_nodes(uint32_t* array, uint32_t count, uint32_t maxRange) { int i; // Set the seed mt_seed(time(NULL) * mt_rand(maxRange)); // Populate the array for (i = 0; i < count; array[i++] = i); // Shuffle the elements int i1, i2; int temp; for (i = 0; i < count; i++) { i1 = (mt_rand(maxRange)); i2 = (mt_rand(maxRange)); temp = array[i1]; array[i1] = array[i2]; array[i2] = temp; } }