大量のデータをファイルに出力しようとしています。現在、次のことを試しています。
byte[][] ハンド、次元は 25 億 x 7
ネストされた一連の for ループがあります。
for ...
for ...
for ...
hands[i][j] = blah
次に、最後に配列 hands のすべてのエントリを出力しています。
別の方法は、メモリを使用せず、毎回書き込むことです: for ... for ... for ... pw.println(blah)
しかし、これは常に印刷されるため、非常に遅くなるようです。
最初のアプローチが最適ですか?k個のエントリごとに保存して印刷するなど、中間的なアプローチの方が良いでしょうか? もしそうなら、使用する k の適切な値は何でしょうか?
編集:これがコードです
package tables;
import general.Config;
import general.Constants;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
// Outputs canonical river hands
public class OutputRiverCanonicalHands3 implements Config, Constants{
public static void main(String[] args) throws IOException {
int half_river = (int)(NUM_RIVER_HANDS/2);
boolean[] river_seen_index_1 = new boolean[half_river];
boolean[] river_seen_index_2 = new boolean[(int)(NUM_RIVER_HANDS - half_river)];
System.out.println("DONE DECLARING RIVER SEEN");
byte hole11, hole12, board1, board2, board3, board4, board5;
long river_index;
byte[][] turnHands = new byte[NUM_TURN_HANDS][6];
System.out.println("DONE DECLARING TURN");
BufferedReader br = new BufferedReader(new FileReader(RIVER_TURN_INDICES_FILE2));
int count = 0;
while (br.ready()) {
StringTokenizer str = new StringTokenizer(br.readLine());
str.nextToken();
for (int i = 0; i < turnHands[count].length; ++i)
turnHands[count][i] = Byte.parseByte(str.nextToken());
++count;
}
br.close();
System.out.println("DONE READING TURN");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(RIVER_CANONICAL_HANDS_FILE3));
byte[][] hands = new byte[half_river][7];
System.out.println("DONE DECLARING RIVER ARRAY");
long startTime = System.currentTimeMillis();
int arrayIndex;
for (int i = 0; i < turnHands.length; ++i) {
if (i % 100000 == 0) {
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println(i + " " + elapsedTime);
}
hole11 = turnHands[i][0];
hole12 = turnHands[i][1];
board1 = turnHands[i][2];
board2 = turnHands[i][3];
board3 = turnHands[i][4];
board4 = turnHands[i][5];
for (board5 = 0; board5 < DECK_SIZE; ++board5) {
if (board5 == hole11 || board5 == hole12
|| board5 == board1 || board5 == board2 || board5 == board3 || board5 == board4)
continue;
river_index = ComputeIndicesTight.compute_river_index(hole11, hole12, board1, board2, board3, board4, board5);
if (river_index < half_river && river_seen_index_1[(int)river_index])
continue;
if (river_index >= half_river && river_seen_index_2[(int)(river_index - half_river)])
continue;
if (river_index < half_river) {
arrayIndex = (int)river_index;
river_seen_index_1[arrayIndex] = true;
hands[arrayIndex][0] = hole11;
hands[arrayIndex][1] = hole12;
hands[arrayIndex][2] = board1;
hands[arrayIndex][3] = board2;
hands[arrayIndex][4] = board3;
hands[arrayIndex][5] = board4;
hands[arrayIndex][6] = board5;
}
else if (river_index == half_river) {
System.out.println("HALFWAY THERE");
for (int j = 0; j < hands.length; ++j)
for (int k = 0; k < 7; ++k)
dos.writeByte(hands[j][k]);
hands = new byte[(int)(NUM_RIVER_HANDS - half_river)][7];
System.out.println("DONE PRINTING HALFWAY!");
}
if (river_index >= half_river) {
arrayIndex = (int)(river_index - half_river);
river_seen_index_2[arrayIndex] = true;
hands[arrayIndex][0] = hole11;
hands[arrayIndex][1] = hole12;
hands[arrayIndex][2] = board1;
hands[arrayIndex][3] = board2;
hands[arrayIndex][4] = board3;
hands[arrayIndex][5] = board4;
hands[arrayIndex][6] = board5;
}
}
}
for (int j = 0; j < hands.length; ++j)
for (int k = 0; k < 7; ++k)
dos.writeByte(hands[j][k]);
dos.close();
}
}