- Home /
need help getting more performance out of this thing
ok so i recently posted a make of HighLife(conways game of life) here: [url]http://forum.unity3d.com/threads/185340-HighLife-%28Conways-Game-Of-Life-Variation%29[/url]
it works wonderfully great. the issue of performance comes in when it reaches a cell count of about 5000. after profiling it comes down to one function that is slowing it down. im curious if anybody has any suggestions to make these functions faster. multithreading i beleive is an option as its not an unity api... but my attempts, if done correctly have shown no gain in fps.
that said this does get about 15 fps with 5000 living cells.
public static void POKECELLS () {
vectors = new List<Vector2>();
foreach (Vector2 vector in cellDict.Keys) {
vectors.Add(vector);
}
foreach (Vector2 vector in vectors) {
cellDict[vector].pokeYourNeighbors();
}
}
which will call from each cell:
public void pokeYourNeighbors () {
//check 1-8 are Vector2 Objcts
Master.POKE(check1);
Master.POKE(check2);
Master.POKE(check3);
Master.POKE(check4);
Master.POKE(check5);
Master.POKE(check6);
Master.POKE(check7);
Master.POKE(check8);
}
and then this last function :
public static void POKE (Vector2 Position) {
if ( cellDict.TryGetValue(Position, out cell)) {
cellDict[Position].neighborCount++;
return;
}
cellDict[Position] = new Ghost(Position);
}
Answer by Loius · Jun 10, 2013 at 09:12 AM
Rip out an expensive array copy from poke cells:
public static void POKECELLS () {
foreach (Vector2 vector in cellDict.Keys) {
cellDict[vector].pokeYourNeighbors();
}
}
p.s.: naming conventions are there for your safety; CamelCase for functions and classes and camelCase for variables will save many heartaches
it originally did that but the foreach loop makes celldict readonly, and the function edits that data.
an attempt to get around this by putting new ghost cells in a temporrary dict only made it slower . but the dictionry is altered in PO$$anonymous$$$$anonymous$$
ah, gotcha - you're doing that right, then, as far as i know. you just have to have a copy for this application.
for actions that will be repeated many times, cutting down function calls and news is a good first step in reducting time cost - it's ugly, but putting all the pokey functions into pokecells (ins$$anonymous$$d of separate functions) should add a small bit of performance
threading is a possibility but i'm not an expert on ti$$anonymous$$g safety; the threading i've done uses messages (sets a bool to true) to indicate that some data is ready to be read. following that pattern (which may be terrible), your thread would build the next frame and then pass tell the main thread to refresh its dictionary from the thread's working dictionary, while the thread creates a new working dictionary.