I run N-particle systems which need a lot of calculations. I always have a frame rate indicator at the top left corner of the screen. With every program I wrote I noticed that it can take up to 20 seconds before the max frame rate is reached.
Can anyone please explain why that is?
Cheers,
Adriaan.
Here is a piece of code that demonstrates it. :
int num = 1_000_000;
void setup() {
size(1000, 800);
frameRate(1000); // maximum possible it won't achieve this
}
void draw() {
background(0);
float sum = 0;
for (int i = 0; i < num; i++) {
float alfa0 = map(i, 0, num, 0, TAU);
float alfa1 = map(i+1, 0, num, 0, TAU);
sum += sin(alfa0) - sin(alfa1);
sum -= cos(alfa0) + cos(alfa1);
}
println(sum);
noStroke();
fill(0);
rect(0, 0, 85, 25);
fill(255);
textSize(40);
text("FPS " + 1000*frameCount / millis(), 2, 40);
}
From the elapsed time you can see we are getting ~60 fps from the get go. This seems to disagree with the Processing reference as shown in the link from the previous post.
Now I have experienced this with Java before and the reason for the performance boost is down to the JVM (Java virtual machine). Now the compiler will convert your source code into byte-code and the JVM will interpret the byte code and execute the appropriate machine code.
Now machine code executes faster than interpreted byte-code so a function that is executed frequently or is CPU intensive would benefit from a one time compilation to machine code rather than repeated interpretation. Now the JVM has a JIT (just-in-time) compiler which will convert such functions into machine code so they can be executed directly rather than interpreted. Generally performance improves as the program executes and more functions are compiled.
This improvement is particularly noticeable with CPU intensive / repetitive tasks e.g. particle systems.
int etime = 0, time;
void setup() {
size(400, 400);
time = millis();
println("Frame rate in setup " + frameRate);
println("Frame Elapsed Frame rate");
println("number time (ms) frames/second");
}
void draw() {
background(0);
etime = millis()-time;
time = millis();
println(frameCount + " " +etime + " " + frameRate);
if (frameCount >= 10) noLoop();
}