Easily measure elapsed time

前端 未结 26 2136
栀梦
栀梦 2020-11-22 04:13

I am trying to use time() to measure various points of my program.

What I don\'t understand is why the values in the before and after are the same? I understand thi

26条回答
  •  有刺的猬
    2020-11-22 04:53

    struct profiler
    {
        std::string name;
        std::chrono::high_resolution_clock::time_point p;
        profiler(std::string const &n) :
            name(n), p(std::chrono::high_resolution_clock::now()) { }
        ~profiler()
        {
            using dura = std::chrono::duration;
            auto d = std::chrono::high_resolution_clock::now() - p;
            std::cout << name << ": "
                << std::chrono::duration_cast(d).count()
                << std::endl;
        }
    };
    
    #define PROFILE_BLOCK(pbn) profiler _pfinstance(pbn)
    

    Usage is below ::

    {
        PROFILE_BLOCK("Some time");
        // your code or function
    }
    

    THis is similar to RAII in scope

    NOTE this is not mine, but i thought it was relevant here

提交回复
热议问题