[英]Game of Life: Tried to increase computation efficiency with new algorithm, reduced in stead. Why?
我正在編寫康威的生命游戲,並制作了一個工作順暢的JS程序。 我在工作版本中做的是檢查網格中每個坐標的每個相鄰坐標,並根據其鄰居數量殺死或生成它。 現在,我想通過跟蹤哪些坐標是活着的,並且只處理那些和它們的鄰居而不是整個網格來使算法更有效。 我做了這個替代計划:
var g = 0;
var cellMatrix = new Array();
var height = 68;
var width = 100;
var livingCellIndex = 0;
var livingCells = new Array();
writeBoard();
declareFirstGeneration();
live();
function live() {
processGeneration();
g++;
setTimeout(live, speed);
}
function declareNextGeneration() {
livingCells[g + 1] = new Array();
cellMatrix[g + 1] = new Array();
for (var x = 0; x < width; x++) {
cellMatrix[g + 1][x] = new Array();
for (var y = 0; y < height; y++) {
cellMatrix[g + 1][x][y] = false;
}
}
}
function declareFirstGeneration() {
livingCells[g] = new Array();
cellMatrix[g] = new Array();
for (var x = 0; x < width; x++) {
cellMatrix[g][x] = new Array();
for (var y = 0; y < height; y++) {
cellMatrix[g][x][y] = false;
}
}
}
function processGeneration() {
declareNextGeneration();
livingCellIndex = 0;
var x, y;
for (var i = 0; i < livingCells[g].length; i++) {
x = livingCells[g][i][0];
y = livingCells[g][i][1];
numberOfNeighbors = getLivingNeighbors(x, y);
//console.log("numberOfNeighbors", numberOfNeighbors);
if (numberOfNeighbors == 2 || numberOfNeighbors == 3) {
spawnCell(g + 1, x, y);
} else {
killCell(g + 1, x, y);
}
for (var neighborX = x - 1; neighborX <= x + 1; neighborX++) {
for (var neighborY = y - 1; neighborY <= y + 1; neighborY++) {
if (neighborX < width && neighborX >= 0 && neighborY < height && neighborY >= 0) {
numberOfNeighbors = getLivingNeighbors(neighborX, neighborY);
//console.log(g, neighborX, neighborY, "has ", numberOfNeighbors, " neighbors");
if (numberOfNeighbors == 3) {
spawnCell(g + 1, neighborX, neighborY);
}
}
}
}
}
refreshGenerationDisplay(x,y);
}
function spawnCell(g, x, y) {
cellMatrix[g][x][y] = true;
livingCells[g][livingCellIndex] = new Array(2);
livingCells[g][livingCellIndex][0] = x;
livingCells[g][livingCellIndex][2] = y;
document.getElementById(x + '-' + y).style.background = "green"; // visual grid
livingCellIndex++;
}
function killCell(g, x, y) {
cellMatrix[g][x][y] = false;
document.getElementById(x + '-' + y).style.background = "none"; // visual grid
}
但我發現它比我的第一個程序要慢得多。 似乎計算每一代的計算成本似乎隨着每一代而增加。 這讓我感到驚訝,因為我在這個替代算法中處理的數據較少。 我不確定它是否有意義,但這是第一個版本:
var g = 0;
var cellMatrix = new Array();
var height = 68;
var width = 100;
declareThisGeneration();
function live() {
g++;
processGeneration();
setTimeout(live, speed);
}
function processGeneration() {
if (oscillation) adjustGForOscillation();
if (g > 0) {
processNormalGeneration();
} else {
processFirstGeneration();
}
}
function processFirstGeneration() {
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
processFirstGenCell(g, x, y);
}
}
}
function processFirstGenCell(g, x, y) {
if (cellMatrix[g][x][y]) { //if alive
spawnCell(g, x, y);
} else { //if dead
killCell(g, x, y);
}
}
function processNormalGeneration() {
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
processCell(g, x, y);
}
}
}
function processCell(g, x, y) {
var livingNeighbors = getLivingNeighbors(g - 1, x, y);
if (cellMatrix[g - 1][x][y]) { //if alive
if (livingNeighbors != 2 && livingNeighbors != 3) {
killCell(g, x, y);
} else {
spawnCell(g, x, y);
}
} else { //if dead
if (livingNeighbors == 3) {
spawnCell(g, x, y);
} else {
killCell(g, x, y);
}
}
}
function spawnCell(g, x, y) {
cellMatrix[g][x][y] = true;
document.getElementById(x + '-' + y).className = 'alive';
}
function killCell(g, x, y) {
cellMatrix[g][x][y] = false;
document.getElementById(x + '-' + y).className = ''
}
我的問題是,是什么讓“改進”的算法如此緩慢,我怎樣才能降低成本呢?
版本1 //首先,最快
版本2 //新,慢
你不斷在新版本中為每一代分配新的數組; 在舊版本中,您將繼續重復使用相同的網格。 除了速度慢之外,如果不是因為速度慢,那么你的內存占用量會不斷增長。
一種可能的解釋是,如果你跟蹤太多事情,計算機將需要分配更多的內存來存儲它們,這需要時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.