I am building a game to teach kids about ecosystems. We are generating the trees from info from a MySQL database about species, age, size, etc. There is a button to run a model that will simulate the real ecosystem going forward in time by one year; ie all the trees will grow, some will die, and some saplings will sprout. It is working now (mostly), but it is too slow (10-15 seconds per simulation year) since we are working with a 4 sq. km patch which has 100k-700k trees in a given year.
I would like to move the computation of the ecosystem model out of Unity and onto one of our servers via a php request (or something else). I was thinking that this would speed up the calculations. Does anyone have any experience doing this/any tips? Could I expect to shave anytime off of the simulation by moving the computation out of Unity?
Just to check: are you certain the bottleneck is in the calculations and not in the creation of gameObjects, procedural editing of meshes, etc? Creating more than a few hundred GameObjects at once can significantly slow down the main Unity thread - this is a bottleneck we’ve run into in our projects.
In terms of speed, Unity runs with a compiled Mono (.NET) backend, so it runs natively on the platform (as opposed to something like Java which runs in a virtual machine and can therefore be much slower). Without knowing your algorithms, it’s hard to say where the bottleneck is - it could be memory allocation, it could be processor speed, it could be calls being blocked by other calls (inefficient code), etc. As syclamoth said, it only helps if your servers are going to solve your bottleneck. Then of course you have to figure out how much information you’ll need to send to the server and what you’ll get back - if you’re trying to seamlessly move through time (rather than discretely change once per year) you may run into latency or bandwidth issues. If you would need to send more than a few kB of data per second, I’d suggest you rethink that route (and remember that ‘bursts’ of data can overwhelm MMO servers).
Have you looked into parallelization? Most computers these days have multiple cores, so you could change your model code to work in parallel. Unity functions (e.g. modifying a Component) aren’t thread safe and can’t be run this way, but pure calculations can definitely be shipped off to other threads.
On a final note: your project sounds very cool. We work in education too, and I’d love a link when you have a stable demo.