繁体   English   中英

用C测量时间

[英]Measuring time in C

我正在为编程课程编写实验作业。 我需要在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;

}

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

我很迷茫,不知道现在应该去哪里(我的任务最简单的部分显示这是最困难的部分...)

clock的分辨率很可能会粗略地衡量findNode调用findNode函数所花费的时间。 通常,对时间进行测量以多次执行此类呼叫,然后将该时间除以执行呼叫的次数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM