从时钟错误的时间

时间:2016-09-23 10:15:42

标签: c time

我有一个计算发送或接收大量数据的时间的程序。当我收到数据时,时钟表示只需要实际需要的时间的一半。

从重现数据的终端输出:

Amount of data recived: 60296112/300000000
Time: 4
Start time: 3269
End time: 4849790
Clocks per sec: 1000000

发送数据的终端的输出:

Sent 300000000 bytes of data
Time to send was 10.793425

发送数据的终端在发送完所有其他数据后会发送停止信号。当我看到接收数据的终端时,我可以看到它在另一个终端开始发送数据时开始计数,我可以看到它从clock()打印输出的时间比输出所说的更长。

接收部分的代码:

static void recive_sock(int socket_fd,char *buffert, size_t buffert_size, struct sockaddr *other, socklen_t *other_size){
    clock_t start_t, end_t;
    long int total_time = 0;
    printf("Listning for data\n" );
    fflush(stdout);
    int run = 1;
    char start = 1;
    int amount = 0;
    int recive_length;

    while(run){
        recive_length = recvfrom(socket_fd, buffert, buffert_size, 0, other, other_size );
        if(recive_length < 0){
            die("Recvfrom failed");
        }
        if(strncmp(buffert, "HELLO!", 6) == 0){
            amount += recive_length;
            if(start == 1){
                start = 0;
                start_t = clock();
                printf("Start: %ld\n",start_t );
            }
            printf("%ld\n",clock() );
        }
        else if (strncmp(buffert, "die", 3) == 0) {
            run = 0;
            end_t = clock();
            printf("End %ld\n",end_t );
            total_time = (end_t - start_t) / CLOCKS_PER_SEC;
            printf("Amount of data recived: %d/%d\nTime: %ld\nStart time: %ld\nEnd time: %ld\n,Clocks per sec: %ld", amount, AMOUNT, total_time, start_t, end_t, CLOCKS_PER_SEC);
        }
    }
} 

1 个答案:

答案 0 :(得分:3)

函数clock将返回可能不是您要查找的CPU时间,而是希望对支持它的系统使用gettimeofdayclock_gettime之类的内容。然后,您可以比较之前和之后的时间以获得经过的时间。对于未来的读者,如果您的系统支持,请使用clock_gettime进行操作:

#include <stdio.h>
#include <time.h>                // for clock_gettime()

int main(void) {
    int i;
    int j;
    int sum = 1;
    struct timespec t1, t2;
    double elapsedTime;

    // start timer
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);

    // do something here
    for (i = 0; i < 10000; i++) {
        for (j = 0; j < 10000; j++) {
            sum *= i+j;
        }
    }

    // stop timer
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);

    // compute and print the elapsed time in millisec
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
    elapsedTime += (t2.tv_nsec - t1.tv_nsec) / 1000000.0;
    printf("%.3f ms elapsed\n", elapsedTime);

    return 0;
}

这是一个关于如何使用gettimeofday测量时间的简单示例(对于高精度定时不太准确):

#include <stdio.h>
#include <time.h>                // for gettimeofday()

int main(void) {
    int i;
    int j;
    int sum = 1;
    struct timeval t1, t2;
    double elapsedTime;

    // start timer
    gettimeofday(&t1, NULL);

    // do something here
    for (i = 0; i < 10000; i++) {
        for (j = 0; j < 10000; j++) {
            sum *= i+j;
        }
    }

    // stop timer
    gettimeofday(&t2, NULL);

    // compute and print the elapsed time in millisec
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
    printf("%.3f ms elapsed\n", elapsedTime);

    return 0;
}