LifeEngine
LifeEngine copied to clipboard
New timing code
#34
Looks like you are running off of the develop branch. Can you either rebase or wait for the develop changes to land?
Agree with @xkortex you should rebase it from develop and set the pr to merge to master, there are updates to the code there. Edit: nevermind, I just needed to change the base branch.
Just checked it out, I'm seeing the same issue that I had on my branch, it seems to slow down much more significantly than the current version with setInterval.
I don't currently have a super consistent benchmark for this, but on my machine it lags to ~15 fps with just a few hundred organisms, much worse than current version. Same problem on my branch.
And this is with the console.log
removed
Ah, I accidentally committed the console.log.
I did not notice the same slowdown. It could be related to Chrome or different hardware, like for example if rendering is slower for you.
One thing that could improve overall performance would be putting the simulation in a web worker.
I think it's in how it's scheduled.
setInterval with a 10ms frame interval will work like the following (100fps)
code-runtime: [ 5ms ]-----[ 5ms ]-----[ 12ms ------- ][ 5ms ]
-------interrupt:|-------------|-------------|------------------| <-- will wait until previous exec is done then interrupt immediately, may catch
back up if next frame is < 8ms
the way it looks like the setTimeout in the code is working will produce the following, setTimeout will set a delay at the end of the simulation runtime of 1000/fps in this case 10ms. offset by the sim time from the beginning of the frame will result in always getting a delay between the end of the last frame and the beginning of the next (read more dead time). code-runtime: [ 5ms ]---------[ 5ms ]---------[ 12ms ------- ]---------[ 5ms ] -------interrupt:|-----------------|------------------|---------------------------| ---setTimeout{10ms}:|-----------------|--------------------------|------------------|
Not going to improve exec through timer manipulation I think, however rendering and simulating in parallel could be a marked improvement (see joonazan above). For more timing analysis see pull request #68.
if you move the "setTimeout" code to the beginning of the updateSimulation() call it will replicate the "setInterval" behavior.
updateSimulation() {
setTimeout(func,1000/fps); //starts counting from start of exec instead of after simulation runs (less dead time)
simulation.update();
}