在Windows中使用C测量执行时间

时间:2016-03-02 09:00:17

标签: c windows time

我想衡量代码和平的执行时间 我正在使用clock()函数 这里的例子是:

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

int main(void) {
    clock_t begin=0, end=0;

    begin = clock();
    printf("hello world!!\n");
    end = clock();

    printf("Begin %E\n",begin);
    printf("End %E\n",end);
    printf("Clocks %E\n",end-begin);
    printf("Clocks per second %E\n", CLOCKS_PER_SEC);
    printf("Time %E\n", (end-begin)/CLOCKS_PER_SEC);

    return 0;
}

这里输出:

hello world!!
Begin 2.529616E-320
End 2.535545E-320
Clocks 5.928788E-323
Clocks per second 4.940656E-318
Time 0.000000E+00

时间是0!
我做错了什么? 输出是否正确?

非常感谢

1 个答案:

答案 0 :(得分:6)

%E需要浮点(floatdouble)。您正在发送整数值

要划分确保号码是浮动的

(端开始+ 0.0)/ CLOCKS_PER_SEC);