Closed SarahBauduin closed 8 years ago
In general, you can do the if( ) statement, if you are able to correctly describe when it is faster. I believe in this case, it is not the size of the world, but the number of agents, i.e., the variable called cellNum, which is the length of the loop element.
Nevertheless, speed is generally of increasing importance as things get larger, so if you are want to write only one option inside the function, it should be the one that scales better at large sizes.
I pushed a slightly different way of making the listAgents object that seems to be about 2x faster than the one you had done. You will have to remove one of them.
I made a if
statement regarding the length(cellNum)
to keep both ways (i.e., with and without the for-loop).
I kept your way for the if(length(cellNum) > 1000)
but I transformed the df back into matrix because the result as to be a list of matrices.
Function
neighbors()
in patch-functions.R Using the for-loop is faster when the world is small (e.g.,createNLworld(0, 10, 0, 10)
). With larger worlds (e.g.,createNLworld(0, 50, 0, 50)
), it is faster without the for-loop. Should I try to find the turning point for the number of patches and make it a if statement in the code (e.g.,if(length(world) < xx){for(...)} else {}
) or do I leave it like this, without the for-loop? Or is there another way of coding it that makes it faster in any case?