【转载】 确定 C 运行时间

原文链接

This post will discuss how to find the execution time of a C program in Windows and Linux environments. 计算 C 的运行时间,Windows 和 Linux 环境。

四种方法。

1. Using clock() function

We can use the clock() function provided by the <time.h> header file to calculate the CPU time consumed by a task within a C application.

It returns the clock_t type, which stores the total number of clock ticks.

To compute the total number of seconds elapsed, we need to divide the total number of clock ticks elapsed by CLOCKS_PER_SEC macro (also present in <time.h>) as shown below:

#include <stdio.h>
#include <time.h>       // for clock_t, clock(), CLOCKS_PER_SEC
#include <unistd.h>     // for sleep()

// main function to find the execution time of a C program
int main()
{
    // to store the execution time of code
    double time_spent = 0.0;
    clock_t begin = clock();
    // do some stuff here
    sleep(3);
    clock_t end = clock();
    // calculate elapsed time by finding difference (end - begin) and
    // dividing the difference by CLOCKS_PER_SEC to convert to seconds
    time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
    printf("The elapsed time is %f seconds", time_spent);
    return 0;
}

2. Using time() function

The <time.h> header also provides time() function that returns the total number of seconds elapsed since the Epoch (00:00:00 UTC, January 1, 1970). It takes the pointer to time_t as an argument, which is usually passed as NULL and returns the time_t type. If the argument is not NULL, then the return value is also stored in the memory pointed by the argument.

Its usage is similar to the clock() function, as shown below:

#include <stdio.h>
#include <time.h>       // for time()
#include <unistd.h>     // for sleep()
 
// main function to find the execution time of a C program
int main()
{
    time_t begin = time(NULL);
    // do some stuff here
    sleep(3);
    time_t end = time(NULL);
    // calculate elapsed time by finding difference (end - begin)
    printf("The elapsed time is %d seconds", (end - begin));
    return 0;
}

3. Using gettimeofday() function

The gettimeofday() function returns the wall clock time elapsed since the Epoch and store it in the timeval structure, expressed as seconds and microseconds.

It is defined in the <sys/time.h> header file and takes two arguments – the first argument is a reference to the timeval structure, and the second argument is a null pointer. The timeval structure is declared as follows by the <time.h> header:

struct timeval {
    long tv_sec;    /* seconds */
    long tv_usec;   /* microseconds */
};

The following code demonstrates the usage of gettimeofday() by measuring the wall clock time:

#include <stdio.h>
#include <sys/time.h>   // for gettimeofday()
#include <unistd.h>     // for sleep()
 
// main function to find the execution time of a C program
int main()
{
    struct timeval start, end;
 
    gettimeofday(&start, NULL);
 
    // do some stuff here
    sleep(5);
 
    gettimeofday(&end, NULL);
 
    long seconds = (end.tv_sec - start.tv_sec);
    long micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
 
    printf("The elapsed time is %d seconds and %d micros\n", seconds, micros);
 
    return 0;
}

This function is supported by GCC compilers and might not work on Windows. 或许无法在Windows上运行。

4. Using clock_gettime() function

We can also use clock_gettime() function defined in <time.h> header file which supports up to nanosecond accuracy. It takes two arguments – the first argument is clock type, and the second argument is a pointer to the timespec structure. The timespec structure is provided by the <time.h> header and is declared as:

struct timespec {
    time_t tv_sec;   /* seconds */
    long tv_nsec;    /* nanoseconds */
};

The following code calculates elapsed time using a system-wide real-time clock, identified by CLOCK_REALTIME, whose time represents seconds and nanoseconds since the Epoch.

#include <stdio.h>
#include <time.h>    // for clock_t, clock()
#include <unistd.h>    // for sleep()
 
#define BILLION  1000000000.0
 
// main function to find the execution time of a C program
int main()
{
    struct timespec start, end;
 
    clock_gettime(CLOCK_REALTIME, &start);
 
    // do some stuff here
    sleep(3);
 
    clock_gettime(CLOCK_REALTIME, &end);
 
    // time_spent = end - start
    double time_spent = (end.tv_sec - start.tv_sec) +
                        (end.tv_nsec - start.tv_nsec) / BILLION;
 
    printf("The elapsed time is %f seconds", time_spent);
 
    return 0;
}

Please note that the clock_gettime() function will work only on very few UNIX machines.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值