可能重複:
How to Calculate Execution Time of a Code Snippet in C++定時語句的執行 - C++
如何,我可以得到的時間由一組特定的一些C++代碼的語句用在何處?
類似於Linux下的time
實用程序,但僅適用於某些特定語句。
可能重複:
How to Calculate Execution Time of a Code Snippet in C++定時語句的執行 - C++
如何,我可以得到的時間由一組特定的一些C++代碼的語句用在何處?
類似於Linux下的time
實用程序,但僅適用於某些特定語句。
如果您正在使用GNU GCC/G ++:
嘗試用--coverage重新編譯,重新運行程序和生成的文件分析與gprof的效用。它還將打印功能的執行時間。
編輯:使用-pg編譯並鏈接,而不使用--coverage,--coverage適用於gcov(哪些行實際執行)。
這裏的代碼很細的片段,在Windows和Linux下正常工作:https://stackoverflow.com/a/1861337/1483826
要使用它,運行它,並保存結果爲「開始時間」和行動後 - 「結束時間」。減去併除以你需要的任何精度。
一個明確的鏈接到另一個答案本身並不是一個答案。請使用「標誌」功能來表示重複的問題。 – Mat
@Mat:我不確定mrowa是否有足夠的聲望可以標記... –
@Mat:我喜歡信用來源,這就是爲什麼我鏈接到片段而不是複製和粘貼 - 仍然,你是關於可能的重複很正確。 (是的,現在標記是從15代表啓用) –
您需要編寫一個簡單的計時系統。在C++中沒有內置的方法。
#include <sys/time.h>
class Timer
{
private:
struct timeval start_t;
public:
double start() { gettimeofday(&start_t, NULL); }
double get_ms() {
struct timeval now;
gettimeofday(&now, NULL);
return (now.tv_usec-start_t.tv_usec)/(double)1000.0 +
(now.tv_sec-start_t.tv_sec)*(double)1000.0;
}
double get_ms_reset() {
double res = get_ms();
reset();
return res;
}
Timer() { start(); }
};
int main()
{
Timer t();
double used_ms;
// run slow code..
used_ms = t.get_ms_reset();
// run slow code..
used_ms += t.get_ms_reset();
return 0;
}
請注意,測量本身會顯着影響運行時間。
您可以使用#inclide <ctime>
標題。 It's functions and their uses are here。假設你想看看代碼花了多少時間。您必須在該部分開始前的當前時間以及該部分結束後的另一個當前時間。然後把這兩個時間的差異。現成功能在ctime
內宣佈完成所有這些工作。只需結賬上面的鏈接。
std::chrono
或boost::chrono
(如果您的編譯器不支持C++ 11)可用於此目的。
std::chrono::high_resolution_clock::time_point start(
std::chrono::high_resolution_clock::now());
....
std::cout << (std::chrono::high_resolution_clock::now() - start);
這是'high_resolution_clock',而不是'high_resolution_timer' –
@BenoitBlanchon謝謝你和錯誤的抱歉,我編輯了我的答案 – BigBoss
您需要自己測量時間。小秒錶類我通常使用這個樣子的:
#include <chrono>
#include <iostream>
template <typename Clock = std::chrono::steady_clock>
class stopwatch
{
typename Clock::time_point last_;
public:
stopwatch()
: last_(Clock::now())
{}
void reset()
{
*this = stopwatch();
}
typename Clock::duration elapsed() const
{
return Clock::now() - last_;
}
typename Clock::duration tick()
{
auto now = Clock::now();
auto elapsed = now - last_;
last_ = now;
return elapsed;
}
};
template <typename T, typename Rep, typename Period>
T duration_cast(const std::chrono::duration<Rep, Period>& duration)
{
return duration.count() * static_cast<T>(Period::num)/static_cast<T>(Period::den);
}
int main()
{
stopwatch<> sw;
// ...
std::cout << "Elapsed: " << duration_cast<double>(sw.elapsed()) << '\n';
}
duration_cast可能不是功能的最佳名字,因爲這個名字的函數的標準庫已經存在。隨意想出一個更好的。 ;)
編輯:請注意,計時是從C + + 11。
可以使用<chrono>
頭中的標準庫:
#include <chrono>
#include <iostream>
unsigned long long fib(unsigned long long n) {
return (0==n || 1==n) ? 1 : fib(n-1) + fib(n-2);
}
int main() {
unsigned long long n = 0;
while (true) {
auto start = std::chrono::high_resolution_clock::now();
fib(++n);
auto finish = std::chrono::high_resolution_clock::now();
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(finish-start);
std::cout << microseconds.count() << "µs\n";
if (microseconds > std::chrono::seconds(1))
break;
}
}
可能重複:How to Calculate Execution Time of a Code Snippet in C++
可以(在http://www.cplusplus.com/reference/clibrary/ctime/更詳細地解釋)使用time.h中C標準庫。下面的程序你想要做什麼:
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
clock_t t1,t2;
t1=clock();
//code goes here
t2=clock();
float diff = ((float)t2-(float)t1)/CLOCKS_PER_SEC;
cout << "Running time: " << diff << endl;
return 0;
}
你也可以這樣做:
int start_s=clock();
// the code you wish to time goes here
int stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
這是不好的。如果你計算每秒鐘的時鐘數,那麼當時鐘速度發生變化時你將會得到不同的結果。該技術自2005年以來一直存在。根據維基百科:https://en.wikipedia.org/wiki/SpeedStep 我不得不降低這一點。 – Joeppie
保存語句開始執行之前的時間,然後讓他們執行,再次獲取時間和。減去第一從它的時間。 –