2018年10月25日木曜日

C++でコード実行時間を測定する

#include <iostream>
#include <chrono>
#include <time.h>
using namespace std;
int N = 10000;
int arr[10000][10000];
void array(){
auto chrono_start = std::chrono::system_clock::now();
/*measure from*/
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
arr[i][j] = j;
}
}
/*measure to*/
auto chrono_end = std::chrono::system_clock::now();
cout << "Elapsed time:"<< (chrono_end-chrono_start).count() <<"[ms]"<< std::endl;
}

int main(void){
array();
} 

C++11 用です。

こういうエラーが出て、C++2011スタンダードが必要ですよとのこと。

In file included from /usr/include/c++/5/chrono:35:0,
                 from time_elapsed.cpp:2:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \

オプションでstdを入れて取り急ぎ解決。

g++ -std=c++11 -o time_elapsed time_elapsed.cpp

0 件のコメント:

コメントを投稿