最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現(xiàn)統(tǒng)計代碼運(yùn)行時間的示例詳解

 更新時間:2023年05月06日 08:21:23   作者:二次元攻城獅  
這篇文章主要為大家詳細(xì)介紹了C++一個有趣的小項目——統(tǒng)計代碼運(yùn)行時間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本來想自己寫的,一看github上面都有就不再重復(fù)造輪子了。github上的項目如下:

純標(biāo)準(zhǔn)庫實現(xiàn)

第一種純標(biāo)準(zhǔn)庫實現(xiàn)的Stopwatch.hpp內(nèi)容如下:

// Copyright Ingo Proff 2017.
// https://github.com/CrikeeIP/Stopwatch
// Distributed under the MIT Software License (X11 license).
// (See accompanying file LICENSE)

#pragma once

#include <vector>
#include <string>
#include <chrono>

namespace stopwatch{

class Stopwatch{
public:
   enum TimeFormat{ NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS };

   Stopwatch(): start_time(), laps({}) {
      start();
   }

   void start(){
      start_time = std::chrono::high_resolution_clock::now();
      laps = {start_time};
   }

   template<TimeFormat fmt = TimeFormat::MILLISECONDS>
   std::uint64_t lap(){
      const auto t = std::chrono::high_resolution_clock::now();
      const auto last_r = laps.back();
      laps.push_back( t );
      return ticks<fmt>(last_r, t);
   }

   template<TimeFormat fmt = TimeFormat::MILLISECONDS>
   std::uint64_t elapsed(){
      const auto end_time = std::chrono::high_resolution_clock::now();
      return ticks<fmt>(start_time, end_time);
   }

   template<TimeFormat fmt_total = TimeFormat::MILLISECONDS, TimeFormat fmt_lap = fmt_total>
   std::pair<std::uint64_t, std::vector<std::uint64_t>> elapsed_laps(){
      std::vector<std::uint64_t> lap_times;
      lap_times.reserve(laps.size()-1);

      for( std::size_t idx = 0; idx <= laps.size()-2; idx++){
         const auto lap_end = laps[idx+1];
         const auto lap_start = laps[idx];
         lap_times.push_back( ticks<fmt_lap>(lap_start, lap_end) );
      }

      return { ticks<fmt_total>(start_time, laps.back()), lap_times };
   }


private:
   typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_pt;
   time_pt start_time;
   std::vector<time_pt> laps;

   template<TimeFormat fmt = TimeFormat::MILLISECONDS>
   static std::uint64_t ticks( const time_pt& start_time, const time_pt& end_time){
      const auto duration = end_time - start_time;
      const std::uint64_t ns_count = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();

      switch(fmt){
      case TimeFormat::NANOSECONDS:
      {
         return ns_count;
      }
      case TimeFormat::MICROSECONDS:
      {
         std::uint64_t up = ((ns_count/100)%10 >= 5) ? 1 : 0;
         const auto mus_count = (ns_count /1000) + up;
         return mus_count;
      }
      case TimeFormat::MILLISECONDS:
      {
         std::uint64_t up = ((ns_count/100000)%10 >= 5) ? 1 : 0;
         const auto ms_count = (ns_count /1000000) + up;
         return ms_count;
      }
      case TimeFormat::SECONDS:
      {
         std::uint64_t up = ((ns_count/100000000)%10 >= 5) ? 1 : 0;
         const auto s_count = (ns_count /1000000000) + up;
         return s_count;
      }
      }
    }
};


constexpr Stopwatch::TimeFormat ns = Stopwatch::TimeFormat::NANOSECONDS;
constexpr Stopwatch::TimeFormat mus = Stopwatch::TimeFormat::MICROSECONDS;
constexpr Stopwatch::TimeFormat ms = Stopwatch::TimeFormat::MILLISECONDS;
constexpr Stopwatch::TimeFormat s = Stopwatch::TimeFormat::SECONDS;

constexpr Stopwatch::TimeFormat nanoseconds = Stopwatch::TimeFormat::NANOSECONDS;
constexpr Stopwatch::TimeFormat microseconds = Stopwatch::TimeFormat::MICROSECONDS;
constexpr Stopwatch::TimeFormat milliseconds = Stopwatch::TimeFormat::MILLISECONDS;
constexpr Stopwatch::TimeFormat seconds = Stopwatch::TimeFormat::SECONDS;


std::string show_times( const std::vector<std::uint64_t>& times ){
    std::string result("{");
    for( const auto& t : times ){
        result += std::to_string(t) + ",";
    }
    result.back() = static_cast<char>('}');
    return result;
}

}

使用示例如下:

//創(chuàng)建一個stopwatch
sw::Stopwatch my_watch;
my_watch.start();

//Do something time-consuming here...

//納秒
std::uint64_t elapsed_ns = my_watch.elapsed<sw::ns>();
//微秒
std::uint64_t elapsed_mus = my_watch.elapsed<sw::mus>();
//毫秒
std::uint64_t elapsed_ms = my_watch.elapsed();
//秒
std::uint64_t elapsed_s = my_watch.elapsed<sw::s>();

類似C#的實現(xiàn)

第二種類似C#的實現(xiàn),StopWatch.h代碼如下:

#ifndef __STOPWATCH_H__
#define __STOPWATCH_H__

#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
#include <Windows.h>
#else
#include <chrono>
#endif

class StopWatch
{
public:
	StopWatch();
	~StopWatch();

	//開啟計時
	void Start();

	//暫停計時
	void Stop();

	//重新計時
	void ReStart();

	//微秒
	double Elapsed();

	//毫秒
	double ElapsedMS();

	//秒
	double ElapsedSecond();

private:
	long long elapsed_;
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
	LARGE_INTEGER start_;
	LARGE_INTEGER stop_;
	LARGE_INTEGER frequency_;
#else
	typedef std::chrono::high_resolution_clock Clock;
	typedef std::chrono::microseconds MicroSeconds;
	std::chrono::steady_clock::time_point start_;
	std::chrono::steady_clock::time_point stop_;
#endif
	
};

#endif // __STOPWATCH_H__

StopWatch.cpp代碼如下:

#include "StopWatch.h"

#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
StopWatch::StopWatch():elapsed_(0)
{
		elapsed_ = 0;
		start_.QuadPart = 0;
		stop_.QuadPart = 0;
		QueryPerformanceFrequency(&frequency_);
}
#else
StopWatch::StopWatch():elapsed_(0),start_(MicroSeconds::zero()),stop_(MicroSeconds::zero())
{
}
#endif

StopWatch::~StopWatch()
{
}

void StopWatch::Start()
{
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
	QueryPerformanceCounter(&start_);
#else
	start_ = Clock::now();
#endif
	
}

void StopWatch::Stop()
{
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
	QueryPerformanceCounter(&stop_);
	elapsed_ += (stop_.QuadPart - start_.QuadPart) * 1000000 / frequency_.QuadPart;
#else
	stop_ = Clock::now();
	elapsed_ = std::chrono::duration_cast<MicroSeconds>(stop_ - start_).count();
#endif
	
}

void StopWatch::ReStart()
{
	elapsed_ = 0;
	Start();
}

double StopWatch::Elapsed()
{
	return static_cast<double>(elapsed_);
}

double StopWatch::ElapsedMS()
{
	return elapsed_ / 1000.0;
}

double StopWatch::ElapsedSecond()
{
	return elapsed_ / 1000000.0;
}

使用示例如下(和C#比較像):

StopWatch sw;
sw.Start();
//Do something time-consuming here...
sw.Stop();
std::cout << "運(yùn)行時間:" << sw.ElapsedMS() << "毫秒" << std::endl;

總結(jié)

  • 如果有代碼潔癖的話就使用第一種,純標(biāo)準(zhǔn)庫實現(xiàn)、功能全面、使用方法偏向傳統(tǒng)C++。
  • 如果不介意使用系統(tǒng)API的話就使用第二種,功能簡單、使用方法偏向傳統(tǒng)C#。

到此這篇關(guān)于C++實現(xiàn)統(tǒng)計代碼運(yùn)行時間的示例詳解的文章就介紹到這了,更多相關(guān)C++統(tǒng)計代碼運(yùn)行時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

宁蒗| 和政县| 都昌县| 全南县| 会宁县| 曲阳县| 安化县| 车险| 湖南省| 朝阳区| 利川市| 二手房| 平舆县| 三台县| 珲春市| 叙永县| 屏东市| 阳信县| 民权县| 衡山县| 沅江市| 隆安县| 玉山县| 洪湖市| 莆田市| 谷城县| 鄱阳县| 西乌珠穆沁旗| 赤水市| 濮阳市| 保定市| 台东市| 上蔡县| 内丘县| 乐昌市| 达尔| 孝昌县| 玛曲县| 安泽县| 永城市| 平昌县|