【问题标题】:How to measure time in ms in c program [closed]如何在c程序中以毫秒为单位测量时间[关闭]
【发布时间】:2013-08-29 15:24:58
【问题描述】:

我需要测量代码的互斥体进入和结束的时间,所以我写了这个:

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>

pthread_t tid[4];
int counter;
pthread_mutex_t lock;

void* doSomeThing(void *arg)
{
    pthread_mutex_lock(&lock);

    time_t stime=time(NULL);

    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFF);i++){
//      printf("%d", i); //this is just wait
    }
    printf("\n Job %d finished\n", counter);

    time_t etime=time(NULL);
    printf("time : %ld\n", difftime(etime, stime));
    pthread_mutex_unlock(&lock);
    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    if (pthread_mutex_init(&lock, NULL) != 0)
    {
        printf("\n mutex init failed\n");
        return 1;
    }

    while(i < 4)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_join(tid[2], NULL);
    pthread_join(tid[3], NULL);
    pthread_mutex_destroy(&lock);

    return 0;
}

但我得到的时间是0

【问题讨论】:

  • 由于difftime 返回一个double,printf 应该是:printf("time : %lf\n", difftime(etime, stime));
  • 根据您优化代码的程度,0 可能是您的计时器分辨率的正确答案。
  • 那可能是因为你的代码运行的太快了,没有可测量的时间差。尝试在你的 for 循环中做一些耗时的事情——例如复杂的数学。
  • @Trenin 或 sleep(10) ?
  • 或者,使用更小的时间粒度。

标签: c++ c multithreading time mutex


【解决方案1】:

有不同的计时方式——“wall time”、“CPU time”是其中的两种。有不同的库可以帮助您执行计时任务。以下是一些:

对于 CPU TIME(如果您在多个 CPU 上有多个线程,这将“比挂钟快”):

#include <time.h>
clock_t startTime, stopTime;
double msecElapsed;
startTime = clock();
// thing that needs timing
stopTime = clock();
msecElapsed = (stopTime - startTime) * 1000.0 / CLOCKS_PER_SEC;

请注意,这可能能够以微秒精度计时 - 取决于编译器和平台。

对于 ELAPSED(挂钟)时间:

#include <sys/timeb.h>

struct timeb start, stop;
ftime(&start);
// thing that needs timing
ftime(&stop);
msecElapsed = timeDifference(&start, &stop);

你还需要这个函数:

double timeDifference(struct timeb *start, struct timeb *stop) {
  return stop->time - start->time + 0.001 * (stim->millitm - start->millitm);
}

如果你使用 OMP 来促进并行处理,它有一个方便的功能

#include <omp.h>
double startTime, stopTime;
startTime = omp_get_wtime();
// do things
stopTime = omp_get_wtime();

这通常是微秒级的精度(即使您没有使用任何其他 OMP 函数)。

最后 - 您可能想查看this earlier questionthis one 的答案以获取更多信息/建议。

【讨论】:

    【解决方案2】:

    您可能会得到零,因为执行所需的时间太小,因此时间戳的分辨率不够精细,正如 @Floris 所建议的那样。

    time() 函数实际上返回自 1970 年 1 月 1 日以来的秒数,而不是毫秒。在快速谷歌之后,似乎可以在 clock() 函数中提供解决方案,该函数提供毫秒级别的时间(未经测试):

    #include <time.h>
    
    int main()
    {
        clock_t start, end;
    
        start = clock();
        // do stuff
        end = clock();
    
        std::cout << "Process took " << (1000 * double(end - start) / CLOCKS_PER_SEC) << "milliseconds." << '\n';
    }
    

    【讨论】:

    • 你的代码不起作用
    • clock()使用的分辨率是系统依赖的,可以根据CLOCK_PER_SEC来确定。由于后者也依赖于系统,因此将两者结合使用可以测量时间。
    • @mezgani:这不是一个很有建设性的(错误)报告。
    • 为编辑干杯@alk
    猜你喜欢
    • 2010-09-26
    • 2015-10-05
    • 2011-12-26
    • 2021-12-19
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多