Efficiency: How to use fewer loops

Hi all, I'm working on a program that uses incoming tweets to draw sections of various svgs.
It works without error, I just don't like how many loops I've ended up using and was wondering how it can be improved.

The top loops are drawing the whole shape and the enhanced loops are drawing the shape sections. The reason for separating them into individual loops was that having them in one loop meant that things were not drawing when the tweet came in and instead everything was drawn simultaneously.

If there is a way to improve this, i'm all ears.

I haven't included the entire program due to size and that this is the only part i'm looking to optimize.


    if (state == S_PLAY) { 
    for (int i = 0; i < numLollipops; i++) {
      shape(lollipopFull, i*SIZE, SIZE, SIZE, SIZE);
      numLollipops = check(i*SIZE, numLollipops);
    for (int i = 0; i < numCircles; i++) {
      shape(circleFull, (width-SIZE)-i*SIZE, SIZE*3, SIZE, SIZE);
      numCircles = check(i*SIZE, numCircles);
    for (int i = 0; i < numLines; i++) {
      shape(lineFull, i*SIZE, SIZE*5, SIZE, SIZE);
      numLines = check(i*SIZE, numLines);
    for (int i = 0; i < numSeptagons; i++) {
      shape(septagonFull, (width-SIZE)-i*SIZE, SIZE*7, SIZE, SIZE);
      numSeptagons = check(i*SIZE, numSeptagons);
    for (int i = 0; i < numSq; i++) {
      shape(squareFull, i*SIZE, SIZE*9, SIZE, SIZE);
      numSq = check(i*SIZE, numSq);
    for (int i = 0; i < numTriStars; i++) {
      shape(starFull, (width-SIZE)-i*SIZE, SIZE*11, SIZE, SIZE);
      numTriStars = check(i*SIZE, numTriStars);
    for (int i = 0; i < numSeptagrams; i++) {
      shape(septagramFull, i*SIZE, SIZE*13, SIZE, SIZE);
      numSeptagrams = check(i*SIZE, numSeptagrams);
    for (Lollipop l : lollipops) l. draw();
    for (Septagon s : septagons) s. draw();
    for (Line li : lines) li.draw();
    for (Circle c : circles) c. draw();
    for (Square sq : squares) sq.draw();
    for (TriStar t : triStars) t. draw();
    for (Septagram sg : septagrams) sg.draw();


  • What does disableStyle() do for each container? I have a feeling they are all related.... maybe they could be handled via a base class? Also, it is important to know what the check() function does...


  • I don't see a way around it if all the shapes are independent.

    The way you are modifying the upper limit of each for loop inside the for loop looks a bit suspect but without more code i can't comment. There's not enough info.

    I do wish they'd fix Java loop syntax because

    for (int i = 0; i < n; i++) {

    is just too verbose and too full of maths for something that's just counting from 0 to n - 1

  • @koogs Each shape is a separate object with its own class (lollipop, septagon, line etc...) and the fullshapes are just PShapes. Would it make it simpler if it utilised inheritance? I know there's not enough info as you said, but could inheritance simplify the amount of loops?

    @kfrajer disableStyle() disables the styling for the svg (each svg image in those functions are labelled as shapenameFull) which allows me to affect svg stroke and color elsewhere in the program.

    The Check function (below) is checking to see if the last drawn shape was off the screen, if so then it starts again. This draws horizontal lines of the fullshapes.

    int check(float x, int num) {
      if (x > width) num = 0;
      return num;
  • that kind of abuse says to me 'use a while loop rather than a for loop'

    or work out how many you need to print beforehand by using width / SIZE

Sign In or Register to comment.