c语言中如何添加计时函数,统计程序执行的时间

这里举一个例子,行优先和列优先遍历二维数组的程序执行时间比较,这里需要调用 time.h
编译环境为vs2013

#include 
#include 
#include 
#define M 10000000
#define N 10

short a[M][N];
void assign_array_rows()
{
    int i, j;
        for (i = 0; ifor (j = 0; j0;
}
void assign_array_cols()
{
    int i, j;

        for (j = 0; jfor (i = 0; i0;
}

int main()
{
    clock_t start, finish;
    double  duration;

    start = clock();
    assign_array_rows();
    finish = clock();
    duration = (double)(finish - start) / CLOCKS_PER_SEC;
    printf("行优先用时: %f seconds\n", duration);

    start = clock();
    assign_array_cols();
    finish = clock();
    duration = (double)(finish - start) / CLOCKS_PER_SEC;
    printf("列优先用时: %f seconds\n", duration);

    system("pause");
    return 0;
}

你可能感兴趣的:(问题)