I knew that PShape is generally faster than immediate mode (begin/endShape), but I didn’t know if editing PShape vertices before drawing ends up slower or faster than the immediate mode. So I tested it with the following codes. I run it a few times and every time the difference was very small - so it seems it doesn’t matter which mode to use when you need to edit the entire vertices.
PShape 136.97ms
immediate mode 142.69ms
(on Windows 10 / Surface Pro 6 i5 / Processing 3.5.4)
PShape:
PShape s;
int frames = 0;
int totalElapsed = 0;
int totalFrames = 100;
int N = 100000;
void setup() {
size(800, 800);
s = createShape();
s.beginShape();
for(int i = 0; i < N; i++) {
float theta = (float)frames * i / N * 2 * PI;
s.vertex(100 * cos(theta), 100 * sin(theta));
}
s.endShape(CLOSE);
}
void draw() {
background(255);
int t = millis();
for(int i = 0; i < N; i++) {
float theta = (float)frames * i / N * 2 * PI;
s.setVertex(i, 100 * cos(theta), 100 * sin(theta));
}
shape(s, 0, 0);
t = millis() - t;
frames++;
if (frames < totalFrames) {
totalElapsed += t;
}
else if (frames == totalFrames) {
println("average time: " + (float)totalElapsed / (float)totalFrames + " ms");
}
}
immediate mode:
PShape s;
int frames = 0;
int totalElapsed = 0;
int totalFrames = 100;
int N = 100000;
void setup() {
size(800, 800);
}
void draw() {
background(255);
int t = millis();
beginShape();
for(int i = 0; i < N; i++) {
float theta = (float)frames * i / N * 2 * PI;
vertex(100 * cos(theta), 100 * sin(theta));
}
endShape(CLOSE);
t = millis() - t;
frames++;
if (frames < totalFrames) {
totalElapsed += t;
}
else if (frames == totalFrames) {
println("average time: " + (float)totalElapsed / (float)totalFrames + " ms");
}
}