zoukankan      html  css  js  c++  java
  • 8 Ways to Measure Execution Time in C/C++

    转载自

    https://levelup.gitconnected.com/8-ways-to-measure-execution-time-in-c-c-48634458d0f9

    #include <chrono>
    class ElapsedTimer
    {
    public:
    	void start() {
    		begin = std::chrono::steady_clock::now();
    	}
    	int64_t nsElapsed() {
    		std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    		return std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();//[ns]
    	}
    	int64_t µsElapsed() {
    		std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    		return std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();//[µs]
    	}
    	int64_t msElapsed() {
    		std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    		return std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();//[ms]
    	}
    	int64_t sElapsed() {
    		std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    		return std::chrono::duration_cast<std::chrono::seconds>(end - begin).count();//[s]
    	}
    	double_t msElapsedf() {
    		std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    		return (std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()) / 1000.0;//[ms .]
    	}
    	double_t sElapsedf() {
    		std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    		return (std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()) / 1000000.0;//[s .]
    	}
    private:
    	std::chrono::steady_clock::time_point begin;
    };
    
  • 相关阅读:
    单调队列
    Johnson全源最短路
    重链剖分
    矩阵快速幂
    Tarjan
    题解 UVA439 骑士的移动 Knight Moves
    题解 SP10500 HAYBALE
    题解 P4058 [Code+#1]木材
    题解 P3395 路障
    题解 SP24 FCTRL2
  • 原文地址:https://www.cnblogs.com/cheungxiongwei/p/14174142.html
Copyright © 2011-2022 走看看