/* * Splay Tree benchmarks */ #include #include #include #include #include #include "timer.h" #include "splay.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 half the 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/50 * sizeof(uint32_t)); generate_random(random, COUNT/50, MAX_RANGE); // Start the timer timer_start(&t); // Get COUNT random numbers into the tree int j; for (j = 0; j < 12; j++) { int i; for (i = 0; i < COUNT/50; get(random[i++], root)); for (i = COUNT/50; i >= 0; get(random[i--], root)); } int i; for (i = 0; i < COUNT/50; get(random[i++], root)); // Stop the timer and print out some statistics timer_end(&t); printf("Retrieved %d elements 25 times front to back to front...:\n", COUNT/50); display_statistics(&t); } /* // Same of for TDRB, AVL and BST 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); } */ int main(void) { Node root = NULL; /* insert(7, makeNode(7), &root); insert(5, makeNode(5), &root); insert(3, makeNode(3), &root); insert(1, makeNode(1), &root); insert(8, makeNode(8), &root); printTree(root); */ test_insert(&root); printf("Height of tree: %d\n", height(root)); test_find(&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; } }