Measuring time in a multithreaded C++ application

前端 未结 2 805
小鲜肉
小鲜肉 2021-01-25 18:30

I\'m writing an application using C++ and OpenMP and I want to reliably (and correctly) measure time of execution of parts of it. I have reviewed a few options (Windows, TDM-GC

2条回答
  •  北海茫月
    2021-01-25 19:00

    Copied directly from my current research project:

    #include 
    #include 
    
    /** @brief Best available clock. */
    using clock_type = typename std::conditional<
      std::chrono::high_resolution_clock::is_steady,
      std::chrono::high_resolution_clock,
      std::chrono::steady_clock>::type;
    

    We want to measure wall time, not user-space CPU cycles to be fair and account for the multi-threading overhead as well. Unfortunately, many implementations define high_resolution_clock as an alias for real_time_clock which would spoil our results in case the system time is adjusted during our measurements.

    Yes, std::chrono is a C++11 feature but if this is research as you say, what stops you from using the most modern compiler? You won't need your code to compile on the most weird platform that might exist somewhere in some dusty cellar of a customer. Anyway, if you just cannot have C++11, you can easily implement these clocks yourself. They are (at least in GNU libstdc++) just thin wrappers around clock_gettime.

提交回复
热议问题