zoukankan      html  css  js  c++  java
  • 如何测试代码执行耗时?

    #include <chrono>
    #include <ctime>
    class Timer
    {
    public:
    	Timer() :running_(false),
    		has_run_at_least_once_(false)
    	{
    
    	}
    	virtual ~Timer()
    	{
    
    	}
    	virtual void Start()
    	{
    		if (!running())
    		{
    			start_cpu_ = std::chrono::high_resolution_clock::now();
    			running_ = true;
    			has_run_at_least_once_ = true;
    		}
    	}
    	virtual void Stop()
    	{
    		if (running())
    		{
    			stop_cpu_ = std::chrono::high_resolution_clock::now();
    			running_ = false;
    		}
    	}
    	virtual float Nanoseconds()
    	{
    		if (!has_run_at_least_once())
    		{
    			printf("Timer has never been run before reading time.");
    			return 0;
    		}
    		return std::chrono::duration_cast<std::chrono::nanoseconds>(stop_cpu_ - start_cpu_).count();
    	}
    	virtual float MilliSeconds()
    	{
    		if (!has_run_at_least_once())
    		{
    			printf("Timer has never been run before reading time.");
    			return 0;
    		}
    		return std::chrono::duration_cast<std::chrono::milliseconds>(stop_cpu_ - start_cpu_).count();
    	}
    	virtual float MicroSeconds()
    	{
    		if (!has_run_at_least_once())
    		{
    			printf("Timer has never been run before reading time.");
    			return 0;
    		}
    		return std::chrono::duration_cast<std::chrono::microseconds>(stop_cpu_ - start_cpu_).count();
    	}
    	virtual float Seconds()
    	{
    		return MilliSeconds() / 1000.0;
    	}
    	inline bool running() { return running_; }
    	inline bool has_run_at_least_once() { return has_run_at_least_once_; }
    private:
    	bool running_;
    	bool has_run_at_least_once_;
    	std::chrono::high_resolution_clock::time_point start_cpu_;
    	std::chrono::high_resolution_clock::time_point stop_cpu_;
    };
    
  • 相关阅读:
    记录。短信网关.
    TP 笔记~
    FUCK IE FLASH(inline hook)
    API HOOK(MessageBoxA)
    inline hook MessageBox(2)
    c#线程中使用 dataset
    匈牙利算法解决二分图最大匹配
    C#:Array类的排序
    C#:属性
    C#:结构
  • 原文地址:https://www.cnblogs.com/cheungxiongwei/p/9087675.html
Copyright © 2011-2022 走看看