C使用clock计算代码执行时间

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

int main()
{
    int i;
    time_t start, stop;
    double duration;
    start = clock();

    long long sum=0;
    for(int i=0;i<1e2;i++){
        sum=sum+i;
        printf("%d,%lld\n",i+1,sum);
    }

    stop =  clock();
    duration = ((double)(stop - start))/CLOCKS_PER_SEC;


    printf("\n duration = %f,%f\n",duration,CLOCKS_PER_SEC);
    return 0;
}
发布了14 篇原创文章 · 获赞 1 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lzdelphi/article/details/104620988