【问题标题】:Wrong time from clock时钟的错误时间
【发布时间】:2016-09-23 10:15:41
【问题描述】:

我有一个程序可以计算发送或接收大量数据的时间。当我收到数据时,时钟说它只需要实际时间的一半左右。

接收数据的终端输出:

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);
        }
    }
} 

【问题讨论】:

  • clock() 返回使用的处理器时间,而不是经过的时间。更多信息:pubs.opengroup.org/onlinepubs/9699919799/functions/…
  • 您确定数据在您开始发送后立即发送出去吗?可能存在延迟发送的内部缓冲,直到有足够的数据发送。我认为发送数据的代码在这里非常相关。
  • 1) if(recive_length &lt; 0){ 在此处检查 EAGAIN 2) if (strncmp(buffert, "die", 3) == 0) { TCP 是基于流的,它不保留“消息边界”,您的“die”关键字可以在缓冲区中的任何位置,甚至可能跨细分市场。

标签: c time


【解决方案1】:

函数 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;
}

【讨论】:

  • (t2.tv_usec - t1.tv_usec) / 1000.0; 注意这个标志。使用绝对差值。
  • 你永远不想使用 gettimeofday 来测量经过的时间。为此目的,这是错误的功能。使用适当的 Posix 函数 - clock_gettime
  • @Xofo 我知道高分辨率计时器,它们更适合测量经过的时间。但是,它不像gettimeofday 那样受到广泛的支持,如果您想在平台支持的准确性之间进行权衡,这是一个不错的选择。我添加了一个示例来演示如何使用clock_gettime
  • > gettimeofday 如果我没记错的话在某些嵌入式系统(如 VxWorks)上不可用,并且不是 Posix 函数,实际上已被 Open Group 弃用(pubs.opengroup.org/onlinepubs/9699919799/functions/…)......我也有在嵌入式板上使用破解版的糟糕体验。
  • @Xofo 哦,有趣。谢谢你告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 2020-07-10
  • 1970-01-01
相关资源
最近更新 更多