Problem: There seems to be a big discrepancy in performance
I recently came back to P5 after not having used it for a few years. I’m just coding.
I grabbed the latest version (2.3.2) and wrote a quick game of life program. It seemed really slow compared to what I expected.
Long story short: I dug out an old version I had written years ago in P5 and compared. After recoding the new one to be code-identical to the original and trying different versions of P5, I discovered that the same GoL program runs about 20 times slower in Ver 2. (60fps in V1, 4fps in V2)
So I started commenting out and removing code to try and find the bottleneck. Below is the minimalist example which exhibits the problem.
let t1, t2;
function setup() {
createCanvas(1024, 640);
}
function draw() {
background(200);
t1 = millis();
for(let y=0; y<height; y++) {
for(let x=0; x<width; x++) {
// Do nothing
}
}
t2 = millis();
const elapsed = t2 - t1 // V.1.11.13 = \~0.3msec, V.2.3.2 = \~17.5msec
text(elapsed.toFixed(2), 3, height-3);
}
In ver. 1.11.13 this takes ~0.3 msecs
In ver. 2.3.2 this takes ~17.5 msecs
That’s almost 60 times more. I’m simply doing two nested for loops over all the pixels, without doing any computation on the pixels. So this is effectively just JavaScript with no P5 functions in the loop, so I don’t understand why it’s so slow.
Is there something I have to do different in the setup of version 2.0? Or is Version 2.0 doing something in the background to slow things down. Any pointers would be great as I’m completely stumped. I tried searching on this forum, but only found references to a couple of graphics functions which were much slower in V2.0.
Extra Detail:
I’m using the minified version in both instances, so it shouldn’t be anything to do the friendly errors.
I’m on Windows, testing in Chrome.
Everything is consistent between the tests other than the P5 version.
Update:
Sorry for the long post, but typing this out actually gave me an idea and I’ve partially answered my question: If I substitute the width and height variables in the loops for hard coded numbers then the loop speeds up to match Ver 1.0. Similarly, I can alias the width and height variables and achieve the same result. So it appears to be something to do with accessing the width and height variables directly.