zoukankan      html  css  js  c++  java
  • NDK时间测量

    在NDK中测量时间,有四种方法。

    LINUX系统方法

    gettimeofday

    以秒和微秒的形式返回自从Epoch(1970-01-01 00:00:00 +0000 (UTC))时间以来,系统已经经过了多少时间。这个函数会受到系统的时间跳变的影响,比如系统管理员重新设置了系统时间。clock_gettime则不受这个的影响(使用特定的时钟时)
    从POSIX.1-2008开始,这个函数被标记为弃用,并推荐使用clock_gettime

    #include <sys/time.h>
    
    //时间通过tv返回
    //tz参数已经被废弃,必须设置为NULL
    //若获取失败,则函数返回-1,否则函数返回0
    int gettimeofday(struct timeval *tv, struct timezone *tz);
    
    struct timeval {
        time_t      tv_sec;     /* seconds */
        suseconds_t tv_usec;    /* microseconds */
    };
    
    //使用示例:
    struct timeval t1, t2;
    gettimeofday(&t1, NULL); //begin
    //do something...
    gettimeofday(&t2, NULL); //end
    int sec = t2.tv_sec - t1.tv_sec;    //秒
    int usec = t2.tv_usec - t1.tv_usec; //微秒
    double ms = sec * 1000.0 + usec / 1000.0; //毫秒
    printf("time:%f ms", ms);
    
    clock_gettime

    以秒和纳秒的形式返回自从Epoch以来经过了多少时间。
    https://linux.die.net/man/2/clock_gettime

    #include <time.h>
    
    //时间通过tp返回
    //clk_id指定使用的clock id
    //若获取成功,函数返回0,失败返回-1
    int clock_gettime(clockid_t clk_id, struct timespec *tp);
    
    struct timespec {
        time_t   tv_sec;        /* seconds */
        long     tv_nsec;       /* nanoseconds */
    };
    //使用
    #include <time.h>
     
    struct timespec time1 = {0, 0};
    struct timespec time2 = {0, 0};
     
    clock_gettime(CLOCK_REALTIME, &time1);
    //do somethings...
    clock_gettime(CLOCK_REALTIME, &time2);
    cout << "time passed is: " << 
        (time2.tv_sec - time1.tv_sec)*1000 + 
        (time2.tv_nsec - time1.tv_nsec)/1000000 << "ms" << endl;
    

    各种时钟:clockid_t

    • CLOCK_REALTIME:测量系统真实的时间,比如wall-clock,会受系统管理员调整系统时间的影响
    • CLOCK_REALTIME_COARSE :CLOCK_REALTIME的快速但低精度版本
    • CLOCK_MONOTONIC:从某个不确定的点开始计时的单调递增的时间,比如开机,不受系统管理调整时间等的时间跳变的影响,但是如果adjtime使得时间增长,那么会受影响,也会受NTP的影响。
    • CLOCK_MONOTONIC_COARSE:CLOCK_MONOTONIC的快速低精度版本
    • CLOCK_MONOTONIC_RAW:与CLOCK_MONOTONIC类似,但是是基于硬件的时间,且不受adjtime和NTP的影响。
    • CLOCK_BOOTTIME:和CLOCK_MONOTONIC类似,但是会包括suspended时间,即suspended也在计时。
    • CLOCK_PROCESS_CPUTIME_ID:高精度的统计进程的CPU时间。
    • CLOCK_THREAD_CPUTIME_ID:统计线程的CPU时间
      image
    time

    获取自从Eroph以来,以秒为单位的时间。

    #include <time.h>
    //返回值:以秒为单位的时间
    //t:若非NULL,则与返回值同义
    time_t time(time_t *t);
    

    语言提供

    clock

    C和C++都提供这个函数

    //C
    //Defined in header <time.h>
    clock_t clock(void);
    
    //C++
    //Defined in header <ctime>
    std::clock_t clock();
    

    这个函数返回程序一个大概的CPU耗时,至于起点则不一定和程序的开始点一致,和具体的实现相关。所以,这个函数单一的调用返回值没有意义,只有两次调用之间的差值才有意义
    这个函数的计时可能比wall clock快或者慢。当本程序与另外的程序共享CPU时,那么就会比wall clock慢;当本程序在多线程执行时,那么就会比wall clock快,因为,比如每一秒内,本程序实际上耗费了多个(多线程)一秒的CPU时间.

    //c++版本
    #include <iostream>
    #include <iomanip>
    #include <chrono>
    #include <ctime>
    #include <thread>
     
    // the function f() does some time-consuming work
    void f()
    {
        volatile double d = 0;
        for(int n=0; n<10000; ++n)
           for(int m=0; m<10000; ++m)
               d += d*n*m;
    }
     
    int main()
    {
        std::clock_t c_start = std::clock();
        auto t_start = std::chrono::high_resolution_clock::now();
        std::thread t1(f);
        std::thread t2(f); // f() is called on two threads
        t1.join();
        t2.join();
        std::clock_t c_end = std::clock();
        auto t_end = std::chrono::high_resolution_clock::now();
     
        std::cout << std::fixed << std::setprecision(2) << "CPU time used: "
                  << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms
    "
                  << "Wall clock time passed: "
                  << std::chrono::duration<double, std::milli>(t_end-t_start).count()
                  << " ms
    ";
    }
    
    //输出:
    CPU time used: 1590.00 ms   //因为本程序多线程,所以比wall clock快
    Wall clock time passed: 808.23 ms
    
    std::chrono::steady_clock

    返回一个单调递增的时间,和CLOCK_MONOTONIC类似,最适合用来测量时间间隔

    #include <iostream>
    #include <vector>
    #include <numeric>
    #include <chrono>
     
    volatile int sink;
    int main()
    {
        for (auto size = 1ull; size < 1000000000ull; size *= 100) {
            // record start time
            auto start = std::chrono::steady_clock::now();
            // do some work
            std::vector<int> v(size, 42);
            sink = std::accumulate(v.begin(), v.end(), 0u); // make sure it's a side effect
            // record end time
            auto end = std::chrono::steady_clock::now();
            std::chrono::duration<double> diff = end-start;
            std::cout << "Time to fill and iterate a vector of " 
                      << size << " ints : " << diff.count() << " s
    ";
        }
    }
    
    //out:
    Time to fill and iterate a vector of 1 ints : 2.43e-07 s
    Time to fill and iterate a vector of 100 ints : 4.1e-07 s
    Time to fill and iterate a vector of 10000 ints : 2.519e-05 s
    Time to fill and iterate a vector of 1000000 ints : 0.00207669 s
    Time to fill and iterate a vector of 100000000 ints : 0.423087 s
    
    
    //简单使用示例:
    #include <chrono>
    #include <ctime>  //CLOCKS_PER_SEC定义
    auto start = std::chrono::steady_clock::now();
    auto end = std::chrono::steady_clock::now();
    printf("time: %f ms", 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC); //打印毫秒
    
    std::chrono::high_resolution_clock::now

    只有C++有,并且从C++11开始. 使用见上例

    #include <chrono>
    static std::chrono::time_point<std::chrono::high_resolution_clock> now() noexcept;
    //since C++11
    
    std::chrono::system_clock

    获取系统时间。

    打印时间 std::ctime
    #include <ctime>
    #include <iostream>
     
    int main()
    {
        std::time_t result = std::time(nullptr);
        std::cout << std::ctime(&result);
    }
    
    //Output:
    Tue Dec 27 17:21:29 2011
    
  • 相关阅读:
    滴滴打车如何成就150亿估值
    互联网专车高补贴开始“退烧”
    城市拥堵加剧,都是互联网快车惹的祸?
    滴滴打车动态加价10-20余元
    专车降价滴滴快车使命终结?
    “专车”监管意见最快本月公布
    专车新规或下周发布,估计有大量司机流失
    滴滴优步神州掀新一轮融资大战
    杭州或率先放开非公司化专车
    恭喜您!获得20元现金红包一个,赶快领取!
  • 原文地址:https://www.cnblogs.com/willhua/p/9411046.html
Copyright © 2011-2022 走看看