zoukankan      html  css  js  c++  java
  • C/C++ 记录时间

    http://stackoverflow.com/questions/2808398/easily-measure-elapsed-time

    https://github.com/picanumber/bureaucrat/blob/master/time_lapse.h








    #include <ctime> void f() { using namespace std; clock_t begin = clock(); code_to_time(); clock_t end = clock(); double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; }

    The time() function is only accurate to within a second, but there are CLOCKS_PER_SEC "clocks" within a second. This is an easy, portable measurement, even though it's over-simplified.

    if (globals::gAMInstance->enableTrans()) {
    clock_t begin = clock();
    std::string ostr;
    raw_string_ostream ostrstream(ostr);
    ostrstream << *module;
    std::ofstream ofs(globals::getOutputDir(ASSEMBLY_BEFORE_TRANS_LL_FILE));
    ofs << ostrstream.str();
    ofs.close();

    outs() << "transforming ...... ";
    legacy::PassManager PM;
    // PM.add(new ProgTrans());
    PM.run(*module);
    outs() << "program transforming finished ";
    outs().flush();
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    std::cerr << "Program transformation spends " << std::fixed
    << std::setprecision(4) << elapsed_secs << "s. ";

    // RWset RW(*globals::gCMInstance->getKModule()->module,
    // shape::RWType::TRANS);
    // RW.dump();

    // RWset rwset(*(this->module), shape::RWType::STATE_CACHE);
    // const Instruction &ins = module->begin()->back().front();
    // errs() << "the live variables of " << ins << " : ";
    // auto s = rwset.readset(ins);
    // s.dump();
    // rwset.dump();
    // exit(0);
    }

    You can abstract the time measuring mechanism and have each callable's run time measured with minimal extra code, just by being called through a timer structure. Plus, at compile time you can parametrize the timing type (milliseconds, nanoseconds etc).

    Thanks to the review by Loki Astari and the suggestion to use variadic templates. This is why the forwarded function call.

    #include <iostream>
    #include <chrono>
    
    template<typename TimeT = std::chrono::milliseconds>
    struct measure
    {
        template<typename F, typename ...Args>
        static typename TimeT::rep execution(F&& func, Args&&... args)
        {
            auto start = std::chrono::steady_clock::now();
            std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
            auto duration = std::chrono::duration_cast< TimeT> 
                                (std::chrono::steady_clock::now() - start);
            return duration.count();
        }
    };
    
    int main() {
        std::cout << measure<>::execution(functor(dummy)) << std::endl;
    }

    Demo

    According to the comment by Howard Hinnant it's best not to escape out of the chrono system until we have to. So the above class could give the user the choice to call count manually by providing an extra static method (shown in C++14)

    template<typename F, typename ...Args>
    static auto duration(F&& func, Args&&... args)
    {
        auto start = std::chrono::steady_clock::now();
        std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
        return std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now()-start);
    } 
    
    // call .count() manually later when needed (eg IO)
    auto avg = (measure<>::duration(func) + measure<>::duration(func)) / 2.0;

    and be most useful for clients that

    "want to post-process a bunch of durations prior to I/O (e.g. average)"


    The complete code can be found here. My attempt to build a benchmarking tool based on chrono is recorded here.


    If C++17's std::invoke is available, the invocation of the callable in execution could be done like this :

    invoke(forward<decltype(func)>(func), forward<Args>(args)...);

    to provide for callables that are pointers to member functions.

  • 相关阅读:
    【高级内部资料】.NET数据批量写入性能分析 第二篇
    负载均衡原理与实践详解 第五篇 负载均衡时数据包流程详解
    负载均衡原理与实践详解 第三篇 服务器负载均衡的基本概念网络基础
    如何提高Linq查询的性能(上)
    【全面解析DeepZoom 之二】Silverlight2及Deep Zoom环境的搭建
    关于让WPF软件界面支持全球化和本地化
    在WPF中自定义控件(3) CustomControl (上)
    【全面解析DeepZoom 之一】酷!Deep Zoom
    谈谈我理解的WPF团队模型——在UI Designer与Developer之间
    [WPF疑难]在WPF中显示动态GIF
  • 原文地址:https://www.cnblogs.com/jjtx/p/5267124.html
Copyright © 2011-2022 走看看