Why does it take some time for my program to get up to speed?

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);
}

Hello @adrinalino,

Scrutinize this code and give it a try:

int num = 1_000_000;

int t2;

void setup() 
  {
  size(1000, 800);
  frameRate(1000); // maximum possible it won't achieve this
  t2 = millis();
  }

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.0*frameCount / millis(), 2, 40);
  text("FrameRate:  " + frameRate, 2, 80);
  text("FrameRate:  " + 1000.0/(millis()-t2), 2, 120);
  t2 = millis();
  }

I know that frameRate gives a rolling average so I used time between frames for real time updates.

Related:

Reference:

:)

Thanks for your answer glv. It helped.
Cheers, Adriaan.

1 Like

If you run the sketch below you will get something like this

Frame rate in setup 60.0
Frame   Elapsed        Frame rate
number  time (ms)     frames/second
1           17         59.915756
2           15         60.09691
3           18         59.921463
4           16         60.00179
5           19         59.641315
6           18         59.495483
7           13         60.06674
8           18         59.91533
9           16         59.968037
10           17         59.95027

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();
}
4 Likes

Reference for framerate from source:

:)

I scoured through some of the old source code and found this in PApplet.java:

Processing 3.4:
public float frameRate = 10;

Processing 3.5:
public float frameRate = 60;

Processing 4.3:
public float frameRate = 60;

It seems that the initial frameRate is now 60 and this is not reflected in the documentation.

:)

1 Like