Created
August 18, 2017 03:47
-
-
Save albow-net/7bbca64cc4808c995904c2020e180218 to your computer and use it in GitHub Desktop.
配列の読み書き速度測定用ソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
int test_array( const int N, const int num ) { | |
// 長さNの配列を用意 | |
int *array = new int[N]; | |
int sum = 0; | |
// 0で初期化 | |
for ( int i = 0; i < N; i++ ) { | |
array[i] = 0; | |
} | |
// 各要素にnumを代入 | |
for ( int i = 0; i < N; i++ ) { | |
array[i] = num; | |
} | |
// 各要素の和を計算 | |
for ( int i = 0; i < N; i++ ) { | |
sum += array[i]; | |
} | |
delete [] array; | |
return sum; | |
} | |
int test_vector( const int N, const int num ) { | |
// 長さNの配列を用意し0で初期化 | |
std::vector<int> vector(N, 0); | |
int sum = 0; | |
// 各要素にnumを代入 | |
for ( auto &a : vector ) { | |
a = num; | |
} | |
// 各要素の和を計算 | |
for ( auto a : vector ) { | |
sum += a; | |
} | |
return sum; | |
} | |
int main(int argc, char *argv[]) { | |
const int N = 1 << 26; | |
const int num = 3; | |
if ( argc < 2 ) return 1; | |
std::string arg = argv[1]; | |
// arrayを指定したら配列の処理を実行 | |
if ( arg == "array" ) { | |
for ( int i = 0; i < 10; i++ ) { | |
std::cout << test_array(N, num) << std::endl; | |
} | |
} | |
// vectorを指定したらvectorの処理を実行 | |
if ( arg == "vector" ) { | |
for ( int i = 0; i < 10; i++ ) { | |
std::cout << test_vector(N, num) << std::endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment