在 C 语言中使用定时器

本文将介绍关于如何在 C 语言中使用定时器的多种方法。

使用 gettimeofday 函数作为定时器基准

gettimeofday 是一个符合 POSIX 标准的函数,用于检索系统时间。它需要两个参数,一个是 struct timeval 类型,一个是 struct timezone 类型,后者现已过时。因此,我们只需要声明 timeval 结构来存储检索到的时间值。struct timeval 由两个成员组成,分别代表秒和微秒。

在下面的例子中,我们实现了两个函数,用于查找整数数组中的最大值。其中一个是基于值比较的,另一个是利用指数来寻找最大值的整数。我们利用 max_ 函数调用前后的 gettimeofday 来比较这些函数的速度。

请注意,有一个 time_diff 函数是以秒为单位计算经过的时间。在这种情况下,我们只在随机生成的整数数组上运行了一次测试,但一般来说,在现代系统中应该使用更多的统计方法来衡量性能。

#include <stdlib.h>#include <string.h>#include <stdio.h>#include <sys/time.h>#include <time.h>int max_index(int arr[], int size){
    size_t max = 0;
    for (int j = 0; j < size; ++j) {
        if (arr[j] > arr[max]) {
            max = j;
        }
    }
    return arr[max];
}
int max_value(int arr[], int size){
    int max = arr[0];
    for (int j = 0; j < size; ++j) {
        if (arr[j] > max) {
            max = arr[j];
        }
    }
    return max;
}
float time_diff(struct timeval *start, struct timeval *end){
    return (end->tv_sec - start->tv_sec) + 1e-6*(end->tv_usec - start->tv_usec);
}
enum {WIDTH = 100000};
int main() {
    struct timeval start;
    struct timeval end;
    int max;
    int *arr = malloc(WIDTH * sizeof(int));
    srand(time(NULL));
    for (size_t i = 0; i < WIDTH; i++) {
        arr[i] = rand();
    }
    gettimeofday(&start, NULL);
    max = max_index(arr, WIDTH);
    gettimeofday(&end, NULL);
    printf ("max_index: %0.8f sec, max = %d\n",
            time_diff(&start, &end), max);
    gettimeofday(&start, NULL);
    max = max_value(arr, WIDTH);
    gettimeofday(&end, NULL);
    printf ("max_value: %0.8f sec, max = %d\n",
            time_diff(&start, &end), max);
    free(arr);
    exit(EXIT_SUCCESS);
}

输出:

max_index: 0.00028346 sec, max = 2147391322
max_value: 0.00022213 sec, max = 2147391322

使用 clock_gettime 函数作为 C 语言中的定时器基准

另外,我们也可以利用 clock_gettime 来实现类似的测量目标。clock_gettime 是一个较新的方法,建议在新的代码库中使用。它将时间值存储在 struct timespec 对象中,并将指向它的指针作为第二个参数。同时,第一个参数指定要使用的时钟类型。在这个例子中,我们检索 CLOCK_REALTIME,因为它测量所谓的挂钟时间。它表示为从时间测量的起始日期-纪元以来经过的秒和纳秒。

#include <stdlib.h>#include <string.h>#include <stdio.h>#include <sys/time.h>#include <time.h>int max_index(int arr[], int size){
    size_t max = 0;
    for (int j = 0; j < size; ++j) {
        if (arr[j] > arr[max]) {
            max = j;
        }
    }
    return arr[max];
}
int max_value(int arr[], int size){
    int max = arr[0];
    for (int j = 0; j < size; ++j) {
        if (arr[j] > max) {
            max = arr[j];
        }
    }
    return max;
}
float time_diff2(struct timespec *start, struct timespec *end){
    return (end->tv_sec - start->tv_sec) + 1e-9*(end->tv_nsec - start->tv_nsec);
}
enum {WIDTH = 100000};
int main() {
    struct timespec start2, end2;
    int max;
    int *arr = malloc(WIDTH * sizeof(int));
    srand(time(NULL));
    for (size_t i = 0; i < WIDTH; i++) {
        arr[i] = rand();
    }
    clock_gettime(CLOCK_REALTIME, &start2);
    max = max_index(arr, WIDTH);
    clock_gettime(CLOCK_REALTIME, &end2);
    printf("max_index: %0.8f sec, max = %d\n",
           time_diff2(&start2, &end2), max);
    clock_gettime(CLOCK_REALTIME, &start2);
    max = max_value(arr, WIDTH);
    clock_gettime(CLOCK_REALTIME, &end2);
    printf("max_value: %0.8f sec, max = %d\n",
           time_diff2(&start2, &end2), max);
    free(arr);
    exit(EXIT_SUCCESS);
}

输出:

max_index: 0.00028346 sec, max = 2147391322
max_value: 0.00022213 sec, max = 2147391322