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

**URL:** <https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309>\
**Category:** Processing\
**Created:** [November 25, 2023, 7:44pm UTC](https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309 "2023-11-25T19:44:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrinalino](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@adrinalino](https://discourse.processing.org/u/adrinalino)\
**Post date:** [November 25, 2023, 7:44pm UTC](https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309/1 "2023-11-25T19:44:24Z")

</div>

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. :

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

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 25, 2023, 8:06pm UTC](https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309/2 "2023-11-25T20:06:54Z")

</div>

Hello @adrinalino,

Scrutinize this code and give it a try:

```auto
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:

> [@FrameRate hiccups](https://discourse.processing.org/t/framerate-hiccups/11947):
>
> I have experienced hiccups in my animations at the start of my animations and trying to understand why this happens. Is it Processing, Java or other? I have reduced it down to a simple example that displays frameRates below a threshold of 54 fps: float threshold = 54; // fps int y = 100; // spacing for text void setup() { size(700, 750, P3D); background(0); textSize(24); } void draw() { // background(0); float temp = frameRate; if (temp \< threshold) { pr…

Reference:

> **[frameRate / Reference](https://processing.org/reference/frameRate.html)**
>
> The system variable frameRate contains the approximate frame rate of the software as it executes. The initial value is 10 fps and is updated with each frame. The value is averaged (integrated) …

`:)`

---

<div class="post-metadata">

**Author:** ![adrinalino](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@adrinalino](https://discourse.processing.org/u/adrinalino)\
**Post date:** [November 26, 2023, 7:55am UTC](https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309/3 "2023-11-26T07:55:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 26, 2023, 1:08pm UTC](https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309/4 "2023-11-26T13:08:39Z")

</div>

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

```auto
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.

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 26, 2023, 1:24pm UTC](https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309/5 "2023-11-26T13:24:23Z")

</div>

Reference for _framerate_ from source:

> <https://github.com/benfry/processing4/blob/main/core/src/processing/core/PApplet.java#L2056>

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 26, 2023, 11:01pm UTC](https://discourse.processing.org/t/why-does-it-take-some-time-for-my-program-to-get-up-to-speed/43309/6 "2023-11-26T23:01:32Z")

</div>

> [@quark](#):
>
> This seems to disagree with the Processing reference as shown in the link from the previous post.

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.

`:)`
