Why does the frameRate drop so much when I load about 100 pictures into the program

hello, i would like to know. why does the frameRate drop so much when I load about 100 pictures into the program. And how to fix it. Pictures from one color, with squares there is no such thing. :neutral_face:

Hi @urlxd,

One need to see your code to be able to analyse…

Cheers
— mnse

1 Like
int size = 100;

int W = 20;
int H = 20;

PImage block;

Cell cell[][] = new Cell[W][H];
void setup (){
  fullScreen(1);
  block = createImage(size,size,RGB);
  block = loadImage("block.png");
  
  for(int x = 0; x < W; x++){
    for(int y = 0; y < H; y++){
      cell[x][y] = new Cell(x,y);
    }
  }
}

void draw (){
  background (0);
  for(int x = 0; x < W; x++){
    for(int y = 0; y < H; y++){
      cell[x][y].make();
    }
  }
  fill(255,0,0);
  text(frameRate, 100,100);
}

class Cell{
  
  int x,y;
  
  Cell(int x, int y){
    this.x = x;
    this.y = y;
  }
  
  void make(){
    image(block, x * size, y * size, size,size);
  }
}

block|1x1

Hi @urlxd,

Please try this. Im not able to test as iam currently only have mobile access …

Cheers
— mnse

int size = 100;

int W = 20;
int H = 20;

PImage block;

Cell cell[][] = new Cell[W][H];
void setup (){
  // use P2D renderer
  fullScreen(P2D, 1);
  // Useless
  //block = createImage(size,size,RGB);
  block = loadImage("block.png");
  // If block not having size size do
  block.resize(size,size);
  
  for(int x = 0; x < W; x++){
    for(int y = 0; y < H; y++){
      cell[x][y] = new Cell(x,y);
    }
  }
}

void draw (){
  background (0);
  for(int x = 0; x < W; x++){
    for(int y = 0; y < H; y++){
      cell[x][y].make();
    }
  }
  fill(255,0,0);
  text(frameRate, 100,100);
}

class Cell{
  
  int x,y;
  
  Cell(int x, int y){
    this.x = x;
    this.y = y;
  }
  
  void make(){
    image(block, x * size, y * size);
  }
}
2 Likes

Hello,

I get 60 fps with your code with:

fullScreen(P2D, 1);

Reference:
https://processing.org/reference/fullScreen_.html

:)

1 Like

Damn typo :grinning:

Thx @glv for correction …

Cheers
---- mnse

1 Like

this can be calculated and stored in the constructor

x2= x * size;
y2= y * size;

and then the image line is just

    image(block, x2, y2);
1 Like

Thanks. I also only have a mobile phone because the PC is weak.

Hi @Chrisir,

This is a reasonable improvement, especially since the issue here is about performance…

However, based on the code above, I wouldn’t do it that way. Since in this case the size is not used for the class at all, I would do it directly at initialization…

cell[x][y] = new Cell(x*size,y*size);

and below just

image(block, x, y);

Cheers
— mnse

2 Likes