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 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 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)); } }