📜  在C / C++中以高精度测量执行时间

📅  最后修改于: 2021-05-08 18:21:12             🧑  作者: Mango

执行时间:给定任务的执行时间或CPU时间定义为系统以其他方式执行该任务所花费的时间,您可以说程序正在运行的时间。
有多种方法可以衡量程序的执行时间,在本文中,我将讨论5种不同的方法来测量程序的执行时间。
测量程序的执行时间。

  1. 在C&C++中使用time()函数

    下面的程序演示如何使用time()函数测量执行时间。

    #include 
    using namespace std;
      
    // A sample function whose time taken to
    // be measured
    void fun()
    {
        for (int i=0; i<10; i++)
        {
        }
    }
      
    int main()
    {
        /* Time function returns the time since the 
            Epoch(jan 1 1970). Returned time is in seconds. */
        time_t start, end;
      
        /* You can call it like this : start = time(NULL);
         in both the way start contain total time in seconds 
         since the Epoch. */
        time(&start);
      
        // unsync the I/O of C and C++.
        ios_base::sync_with_stdio(false);
      
        fun();
      
        // Recording end time.
        time(&end);
      
        // Calculating total time taken by the program.
        double time_taken = double(end - start);
        cout << "Time taken by program is : " << fixed
             << time_taken << setprecision(5);
        cout << " sec " << endl;
        return 0;
    }
    
    输出:
    Time taken by program is : 0.000000 sec
    
  2. 在C和C++中使用clock()函数。

    下面的程序演示了如何使用clock()函数测量执行时间。您也可以看到此代码

    #include 
    using namespace std;
      
    // A sample function whose time taken to
    // be measured
    void fun()
    {
        for (int i=0; i<10; i++)
        {
        }
    }
      
    int main()
    {
        /* clock_t clock(void) returns the number of clock ticks
           elapsed since the program was launched.To get the number 
           of seconds used by the CPU, you will need to divide by 
           CLOCKS_PER_SEC.where CLOCKS_PER_SEC is 1000000 on typical
           32 bit system.  */
        clock_t start, end;
      
        /* Recording the starting clock tick.*/
        start = clock();
      
        fun();
      
        // Recording the end clock tick.
        end = clock();
      
        // Calculating total time taken by the program.
        double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
        cout << "Time taken by program is : " << fixed 
             << time_taken << setprecision(5);
        cout << " sec " << endl;
        return 0;
    }
    
    输出:
    Time taken by program is : 0.000001 sec
    
  3. 在C和C++中使用gettimeofday()函数。

    下面的程序演示如何使用gettimeofday()函数测量执行时间。

    #include 
    #include 
    using namespace std;
      
    // A sample function whose time taken to
    // be measured
    void fun()
    {
        for (int i=0; i<10; i++)
        {
        }
    }
      
    int main()
    {
        /* The function gettimeofday() can get the time as
           well as timezone. 
           int gettimeofday(struct timeval *tv, struct timezone *tz);
          The tv argument is a struct timeval and gives the
          number of seconds and micro seconds since the Epoch.
          struct timeval {
                   time_t      tv_sec;     // seconds 
                   suseconds_t tv_usec;    // microseconds
               };    */
        struct timeval start, end;
      
        // start timer.
        gettimeofday(&start, NULL);
      
        // unsync the I/O of C and C++.
        ios_base::sync_with_stdio(false);
      
        fun();
      
        // stop timer.
        gettimeofday(&end, NULL);
      
        // Calculating total time taken by the program.
        double time_taken;
      
        time_taken = (end.tv_sec - start.tv_sec) * 1e6;
        time_taken = (time_taken + (end.tv_usec - 
                                  start.tv_usec)) * 1e-6;
      
        cout << "Time taken by program is : " << fixed
             << time_taken << setprecision(6);
        cout << " sec" << endl;
        return 0;
    }
    
    输出:
    Time taken by program is : 0.000029 sec
    
  4. 在C和C++中使用clock_gettime()函数。

    下面的程序演示如何使用clock_gettime()函数测量执行时间。

    #include 
    #include 
    using namespace std;
      
    // A sample function whose time taken to
    // be measured
    void fun()
    {
        for (int i=0; i<10; i++)
        {
        }
    }
      
    int main()
    {
        /* int clock_gettime( clockid_t clock_id, struct 
         timespec *tp ); The clock_gettime() function gets
         the current time of the clock specified by clock_id, 
         and puts it into the buffer  pointed to by tp.tp 
         parameter points to a structure containing 
         atleast the following members:    
         struct timespec {
                   time_t   tv_sec;        // seconds 
                   long     tv_nsec;       // nanoseconds
               };
        clock id = CLOCK_REALTIME, CLOCK_PROCESS_CPUTIME_ID, 
                   CLOCK_MONOTONIC ...etc
        CLOCK_REALTIME : clock  that  measures real (i.e., wall-clock) time.
        CLOCK_PROCESS_CPUTIME_ID : High-resolution per-process timer 
                                   from the CPU.
        CLOCK_MONOTONIC : High resolution timer that is unaffected
                          by system date changes (e.g. NTP daemons).  */
        struct timespec start, end;
      
        // start timer.
        // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
        // clock_gettime(CLOCK_REALTIME, &start);
        clock_gettime(CLOCK_MONOTONIC, &start);
      
        // unsync the I/O of C and C++.
        ios_base::sync_with_stdio(false);
      
        fun();
      
        // stop timer.
        // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
        // clock_gettime(CLOCK_REALTIME, &end);
        clock_gettime(CLOCK_MONOTONIC, &end);
      
        // Calculating total time taken by the program.
        double time_taken;
        time_taken = (end.tv_sec - start.tv_sec) * 1e9;
        time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;
      
        cout << "Time taken by program is : " << fixed
             << time_taken << setprecision(9);
        cout << " sec" << endl;
        return 0;
    }
    
    输出:
    Time taken by program is : 0.000028 sec
    
  5. 在C++中使用chrono::high_resolution_clock

    下面的程序演示如何使用high_resolution_clock函数测量执行时间。有关chrono库的详细信息,请参阅此

    #include 
    #include 
    using namespace std;
      
      
    // A sample function whose time taken to
    // be measured
    void fun()
    {
        for (int i=0; i<10; i++)
        {
        }
    }
      
    int main()
    {
        auto start = chrono::high_resolution_clock::now();
      
        // unsync the I/O of C and C++.
        ios_base::sync_with_stdio(false);
      
        fun();
      
        auto end = chrono::high_resolution_clock::now();
      
        // Calculating total time taken by the program.
        double time_taken = 
          chrono::duration_cast(end - start).count();
      
        time_taken *= 1e-9;
      
        cout << "Time taken by program is : " << fixed 
             << time_taken << setprecision(9);
        cout << " sec" << endl;
        return 0;
    }
    
    输出:
    Time taken by program is : 0.000024 sec
    

    想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。