算法竞赛入门经典学习笔记——程序运行时间统计

程序运行时间统计

程序结束前,调用

printf("Time used = %.2lf\n", (double)clock()/CLOCKS_PER_SEC);//头文件

可获得程序的运行时间, 时间以秒为单位

#include
#include

const int MOD = 1000000;
int i, j, n;

void alg_1(){
    int S = 0;
    for(i = 1; i <= n; i++){
        int fac = 1;
        for(j = 1; j <= i; j++){
            fac = (fac*j)%MOD;
        }
        S = (S + fac)%MOD;
    }
    printf("alg_1 = %d\n", S);
    printf("Time used = %.2lf\n", (double)clock()/CLOCKS_PER_SEC);
}

void alg_2(){
    int S = 0;
    int fac = 1;
    for(i = 1; i <= n; i++){
        fac = (fac*i)%MOD;
        S = (S + fac)%MOD;
    }
    printf("alg_2 = %d\n", S);
    printf("Time used = %.2lf\n", (double)clock()/CLOCKS_PER_SEC);
}


int main(){
    scanf("%d", &n);
    printf("Time used = %.2lf\n", (double)clock()/CLOCKS_PER_SEC);
    alg_1();
    alg_2();
    return 0;

}

 

 

你可能感兴趣的:(算法竞赛入门经典学习笔记——程序运行时间统计)