Using clock() to measure time in C

时间:2016-11-26 18:39:01

标签: c time

I've written this code and it does not return the proper time (probably some piece of the code is wrong). And is there any other way to measure time between two actions more precisely then this (in c)?

float measureTime(clock_t start, clock_t end){

    float totalSeconds;    

    totalSeconds = ((long double)(end - start))/CLOCKS_PER_SEC;

    printf("%.6f", totalSeconds);

    return(totalSeconds);
}

int main(){

    clock_t start, end;

    start = clock();
    //piece of code here
    //
    end = clock();

    measureTime(start, end);

    return (EXIT_SUCCESS);
}

1 个答案:

答案 0 :(得分:0)

正如Weather Vane所说,有很多关于时间测量的问题。这是一个How do I measure time in C?

只是对你的方法发表评论,如果你的作品代码包含很少的执行行列,你可能会得到0.0s(如果你使用的是具有更高频率的4 cpus的现代计算机,那么这段代码将被执行几个时钟周期。)