You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.0 KiB

package de.hsfulda.pmuw.oop;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
public class CgolBoardOop {
private final Collection<CgolCell> activeCells ;
private final CellCreator cellCreator;
private boolean isOptimized;
CgolBoardOop(CellCreator cellCreator, boolean isOptimized) {
this.cellCreator = cellCreator;
this.isOptimized = isOptimized;
activeCells = isOptimized? new HashSet<>():new ArrayList<>();
}
public void initialize() {
cellCreator.createCells(activeCells, isOptimized);
}
public void nextGen() {
List<CgolCell> currentGeneration = new ArrayList<>(activeCells);
if (isOptimized)
activeCells.clear();
currentGeneration.stream().parallel().forEach(CgolCell::calculateNextGeneration);
currentGeneration.stream().parallel().forEach(CgolCell::switchState);
}
private long countAliveCells() {
return activeCells.stream().filter(CgolCell::isAlive).count();
}
public static void main(String[] args) {
boolean isOptimized = true;
startGame(!isOptimized);
startGame(isOptimized);
}
private static void startGame(boolean isOptimized) {
long start = Calendar.getInstance().getTimeInMillis();
CgolBoardOop cgolBoard = new CgolBoardOop(new CellCreator(), isOptimized);
cgolBoard.initialize();
long end = Calendar.getInstance().getTimeInMillis();
System.out.println(String.format("initializing took: %sms", end - start));
start = Calendar.getInstance().getTimeInMillis();
int iterations = 1000;
for (int i = 0; i < iterations; i++) {
cgolBoard.nextGen();
if (0 == i % (isOptimized? 100:100))
System.out.println(String.format("iteration %6d cells alife: %7s , cells active %7s",i,cgolBoard.countAliveCells(),cgolBoard.activeCells.size()));
}
end = Calendar.getInstance().getTimeInMillis();
System.out.println(
String.format("time spend for %d iterations: %sms, optimized: %s\n\n", iterations, end - start, isOptimized));
}
}