Why is square(x, y, 1) so much faster than point(x, y)?

Anyone knows? I’ve found about it by accident and it doesn’t make much sense to me.

Details:
stroke(someColor); point(x, y);
vs.
noStroke(); fill(someColor); square(x, y, 1);

  • the second one is consistently about 3-4 times faster than the 1st
  • rect(x, y, 1, 1) can be used instead of square
  • the usage of e.g. circle(x, y, 1) will not lead to speed improvement
  • it doesn’t work without noStroke()
  • default renderer used, but I think it works on all the others (needs more testing)
  • tested on Processing 4.2
1 Like

Not sure about Java mode because that uses Java AWT to draw graphics but in P2D and P3D the underlying renderer is OpenGL.

Now OpenGL is optimized to draw render images using triangles and is much slower rendering lines so rendering any shape with a stroke will be significantly slower than a shape with noStroke.

I am not sure about circle because OpenGl can represent circles and ellipses as a series of connected triangles (TRIANGLE_FAN) but I don’t know how Processing implements circle and ellipse.

For speed avoid using stroke whenever possible.

2 Likes

Thanks.
I’ve begun replacing point() with square() in my sketches wherever possible and needed, the gain in speed is huge.
I’ve tried the same with Processing for Android, it’s still faster but barely.

1 Like

I know why.

It’s because the point is not really a “point”. It’s a small mesh that is oriented to face the camera.

If you are drawing square points, the mesh is a quad, or two triangles. If you are drawing round points, the mesh is a set of triangles that share one vertex.

When Processing (P2D or P3D) draws a square point, it is the same number of triangles as when it draws a quad (rectangle). When it draws a round point, it is about the same number of triangles as a circle.

There are other details that can affect performance, such as when you use beginShape(POINTS) and repeatedly call vertex() vs calling point() repeatedly. These issues are secondary to the above issue though.

The downside of using square(x, y, 1) is that you have to make sure the squares are oriented towards the camera. If you look at the square from a shallow angle, you won’t see anything.

If you need round points and also need performance, you can draw square points but then use a shader to make them look round.

3 Likes

Thanks for clearing some things up.
There is also a related discussion here:

1 Like