《想到什么更新什么系列》processing 性能优化

·Image相关

image(img,x,y,width,height),图片过多时会导致掉帧,可以考虑在一开始的时候使用resize()方法进行缩放,然后显示缩放后的图片

void setup(){
    img = loadImage("...");
}

void draw(){
    image(img,0,0,20,20);
}

||
/

void setup(){
    img = loadImage("...");
    img.resize(20,20);
}

void draw(){
    image(img,0,0);
}

·Shape相关

1.能够noStroke()的,尽量noStroke(),画布存在大量ellipse时,stroke的绘制会消耗大量计算。

2.能够Ellipse的,尽量不用point()

void setup(){
  size(500,500);
}

void draw(){
  background(255);
  noStroke();
  fill(255,0,0);
  for(int i = 0 ; i < 5000; i ++){
    circle(random(width),random(height),10);
  }
  surface.setTitle(frameRate + "");
}
对比:
void setup(){
  size(500,500);
}

void draw(){
  background(255);
  strokeWeight(10);
  stroke(255,0,0);
  for(int i = 0 ; i < 5000; i ++){
    point(random(width),random(height));
  }
  surface.setTitle(frameRate + "");
}

·Sketch相关

有时候加上P2D之后,你可能不需要考虑其他优化问题了,虽然它有可能降低一些渲染精度。细节参照:https://processing.org/tutorials/rendering/

·语言相关

能用for的尽量不用foreach

能用[]尽量不用Collections

原文地址:https://www.cnblogs.com/CodeSnippet/p/10607121.html