用C测量时间

时间:2014-01-27 10:54:50

标签: c time

我正在编写一个编程类的实验室作业。我需要在C :)中完成它。我选择的任务如下:我在C中创建了三种数据类型:链表,二叉树和avl树。所有数据处理5,000,000个元素。我需要测量在某个步骤中找到并找不到元素所需的时间。我有正确和负面发现的固定值。

以下是我的一些代码,其中包含用于创建5,000,000个数字的数组的函数以及用于添加和查找节点的二元树的两个函数。我希望代码很清楚。

#include <stdio.h>
#include <stdlib.h>

#define SIZE 5000000

typedef struct Node {

    int data;
    struct Node *left, *right;
} Node;

void evenArrayGen(int num, int even_nums[]);
void shuffle(int numbers[]);
void addNode(Node **root, int value);
Node* findNode(Node *root, int value);



/** Recursively adds given value to the tree
 *
 * @param root
 * @param value
 *
 */

void addNode(Node **root, int value) {

    Node *temp;
    ///check statement to avoid infinite loop in adding
    if(*root == NULL) {
        temp = (Node*)malloc(sizeof(Node));
        temp->data = value;
        temp->left = temp->right = NULL;
        *root = temp;
        return;
    }
    ///recursively check where to add node
    ///if smaller than the root add to the left
    ///if bigger than the root add to the right
    else {
        temp = *root;
        if(value < temp->data) {
            addNode(&(temp->left), value);
        } else {
            addNode(&(temp->right), value);
        }
        return;
    }
}


/** Recursively searches for Node with given value
 *
 * @param root
 * @param value
 * @return Node or NULL (if Node was not found)
 *
 */

Node* findNode(Node *root, int value) {

    Node *temp;
    ///check statement to avoid infinite loop in searching
    ///if it reachese that point given value is not in the tree
    if(root == NULL) {
       // printf("Given value is not in the tree\n");
        return NULL;
    }
    temp = root;
    if(temp->data == value) {
       // printf("Searched value is: %i\n", temp->data);
        return temp;
    } else if(value < temp->data) {
        findNode(temp->left, value);
    } else {
        findNode(temp->right, value);
    }
}


/** Generates array of ordered even numbers from 2
 *
 * @param num number of uniqe even digits
 * @param array of size equal to the number
 *
 */

void evenArrayGen(int num, int even_nums[]) {

    int i, current;
    i = current = 0;
    for(; i<num; i++) {
        even_nums[i] = current += 2;
    }
    return;
}


/** Shuffle the array in random order. Radomly gets the index between 0 and
 *  current last index. Swaps number at random index with last one and
 *  decreses the current_last index by 1.
 *
 * @param array of numbers to be shuffled
 *
 */

void shuffle(int nums[]) {

    int i, len, current_last, index, temp;
    ///set the current_last to length of the array to track index for
    ///swaping nums
    current_last = len = SIZE;
    for (i=0; i<len; i++) {
        srand(time(NULL));
        index = rand()%(current_last);
        temp = nums[index];
        nums[index] = nums[current_last];
        nums[current_last] = temp;
        current_last--;
    }
    return;

}

int main() {

    //initialize root for the tree
    Node *root;
    //intilialize array of 5,000,000 elements, and scores
    static int nums[SIZE];
    int i; //iterator
    //initialize positive and negative numbers for find method
    int positive_num, negative_num;
    //initilaize timer
    clock_t start_for_adding, start_for_finding;


    //add elements to the array
    evenArrayGen(SIZE, nums);
    shuffle(nums);
    //set positive number to one of the first 5000 elements
    positive_num = nums[3222];
    negative_num = 345; //all numbers in num are even so negative need to be odd

    root = NULL; //set the root Node to NULL
    start_for_adding = clock(); //zero the timer

    //now iterate trough all elements in nums array and add each to
    //the binary tree
    for(i=0; i<SIZE; i++) {

        //check if i reached proper value
        if (i == 5000 || i == 50000 || i == 500000 || i == (5000000-1)) {
            //add the adding time
            printf("\nIndex: %d\n", i);
            printf("Adding: %.5f\n", (double) clock() - start_for_adding);
            start_for_finding = clock(); //zero the timer
            //search for positive num
            findNode(root, positive_num);
            printf("Finding positive: %.5f\n", 
                   (double) clock() - start_for_finding);
            start_for_finding = clock(); //zero the timer
            //search for negative num
            findNode(root, negative_num);
            printf("Finding negative: %.5f\n", 
                   (double) clock() - start_for_finding);
            start_for_adding = clock(); //reset the timer for adding
        }
        addNode(&root, nums[i]);
    }
    return;

}

查找元素的时间指向零(在两种情况下)

我很迷茫,不知道现在要去哪里(我的任务中最简单的部分显示最难的......)

1 个答案:

答案 0 :(得分:0)

clock的分辨率最有可能是粗略的,以衡量个人拨打findNode功能所需的时间。通常,测量时间是为了多次执行此类呼叫,然后将此时间除以呼叫执行的次数。