Last active
August 18, 2017 03:53
-
-
Save albow-net/b41afa619b0759c105575824c83af0e2 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
import java.util.ArrayList; | |
import java.util.Collections; | |
public class test{ | |
private static int test_arraylist( int N, int num ) { | |
// 長さNの配列を用意し0で初期化 | |
ArrayList<Integer> array = new ArrayList<Integer>( Collections.nCopies(N, 0) ); | |
// 各要素にnumを代入 | |
for ( int i = 0; i < N; i++ ) { | |
array.set(i, num); | |
} | |
// 各要素の和を計算 | |
int sum = 0; | |
for ( int i = 0; i < N; i++ ) { | |
sum += array.get(i); | |
} | |
return sum; | |
} | |
private static int test_array( int N, int num ) { | |
// 長さNの配列を用意 | |
int[] array = new int[N]; | |
// 0で初期化 | |
for ( int i = 0; i < N; i++ ) { | |
array[i] = 0; | |
} | |
// 各要素にnumを代入 | |
for ( int i = 0; i < N; i++ ) { | |
array[i] = num; | |
} | |
// 各要素の和を計算 | |
int sum = 0; | |
for ( int i = 0; i < N; i++ ) { | |
sum += array[i]; | |
} | |
return sum; | |
} | |
public static void main(String[] args) { | |
final int N = 1<<26; | |
final int num = 3; | |
if ( args.length < 1 ) return; | |
// arrayを指定したら配列の処理を実行 | |
if ( args[0].equals( "array" ) ) { | |
for ( int i = 0; i < 10; i++ ) { | |
System.out.println( test_array( N, num ) ); | |
} | |
} | |
// arraylistを指定したらArrayListの処理を実行 | |
else if ( args[0].equals( "arraylist" ) ) { | |
for ( int i = 0; i < 10; i++ ) { | |
System.out.println( test_arraylist( N, num ) ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment