Displaying text makes my frameRate fall

edited October 2015 in Android Mode

Hello, I have a little problem. On a processing project for android, I have to display a lot of text on the screen. I use :

string myString = "lots of words";
textFont(myFont, 20);
text(myString, x, y);

but the performances are very low (5 fps). Does anyone has an advice for a best performance?

Thanks a lot, JM

Answers

  • edited October 2015
    • If you're using 1 PFont only, use textFont() in setup() only.
    • If its size doesn't change use textSize() in setup() only.
    • If all text() are displayed at the same coordinates, make a PGraphics for it in setup().
  • edited October 2015

    Thanks, I tried to use textFont() in setup() only but that didn't change anything... I can't use PGraphics bcause my texts are not always at the same place. I forgot to tell I used boxes to draw the text, well I mean : text(myString, x, y, width, height);

  • "lots of words"

    define lots.

    i've had problems before (early apple macs) where the word-wrapping algorithm part of the text displayer would flood the cpu dragging down performance. it's non-trivial, especially with proportionally spaced fonts.

    I can't use PGraphics bcause my texts are not always at the same place.

    you can move PGraphics...

  • lots of words : almost 3000 characters...

  • think we're going to need a short, runnable example that shows the problem just so that we can check you're not doing something stupid and to save us having to guess implementation details.

  • OK, here's my code :

    package processing.test.texte;
    import processing.core.*;
    public class texte extends PApplet {
    
      String texte1;
      float offset;
    
      public void setup() {
        background(0);
        texte1 = "Iz a d\u00e9barqu\u00e9 dans le Group alors que j'\u00e9touffais dans ma petite vie. Il a pris ma d\u00e9fense contre le Sup\u00e9rieur qui m'accusait d'avoir un mauvais esprit. Il est vrai que je me moquais de lui pendant ses le\u00e7ons, cet imb\u00e9cile nous inculquait des mati\u00e8res inutiles telles que <<Le mouvement de l'Histoire>> qui m'ennuyait \u00e0 mourir.\n\nSa le\u00e7on consistait \u00e0 d\u00e9montrer la sup\u00e9riorit\u00e9 de la Nouvelle D\u00e9mocratie, par un implacable raisonnement, oublieux de certains d\u00e9tails historiques. La notion de <<d\u00e9mocratie>> devait se r\u00e9duire - \u00e0 l'entendre - \u00e0 la d\u00e9mocratie repr\u00e9sentative, gardant sous silence le mod\u00e8le grec fond\u00e9 par Solon qui, d'ailleurs, n'\u00e9tait plus enseign\u00e9 depuis pr\u00e8s d'un mill\u00e9naire.\n\nCette \u00e9troitesse du raisonnement me d\u00e9sesp\u00e9rait. Etais-je le seul \u00e0 voir que, sous ses airs amicaux, la soci\u00e9t\u00e9 nous enfermait dans un syst\u00e8me reproductif o\u00f9 les Assis engendraient des Assis? O\u00f9 la propri\u00e9t\u00e9 \u00e9tait un vol et la domination de l'Homme par l'Homme une provocation.\n\nPuis Iz est arriv\u00e9 dans le Group. Ce jeune d\u00e9briefeur avait \u00e9t\u00e9 \u00e9loign\u00e9 de la capitale par le gouvernement pour ses id\u00e9es subversives. Non pas qu'il remit en cause l'id\u00e9ologie du Premier Homme mais il pensait qu'au lieu de nous d\u00e9briefer sur des faits historiques, le Group devait laisser aux apprenants le soin de se faire leur propre opinion en \u00e9tudiant ce qu'il appelait la petite histoire, celle des domin\u00e9s. Sa p\u00e9dagogie se voulait novatrice, il avait test\u00e9 sur les enfants des Assis une p\u00e9dagogie contributive o\u00f9 chacun partageait ses connaissances avec autrui. Mais les parents n'avaient pas appr\u00e9ci\u00e9 cette perte de temps et Iz fut envoy\u00e9 dans une lointaine banlieue, au milieu des Debouts. C'est ainsi que je l'ai vu d\u00e9barquer dans notre petit Group. Le soir, je le raccompagnais \u00e0 sa cellule, nos discussions anim\u00e9es prolongeaient ce que nous avions \u00e9tudi\u00e9 en cours. J'avais 15 ans et lui 21.\n\nUn jour, Iz m'a ouvert ses connexions au r\u00e9seau et initi\u00e9 \u00e0 l'\u00e9criture num\u00e9rique. C'est comme \u00e7a que j'ai d\u00e9couvert les cyberpo\u00e8mes de Saul. J'ai tout visionn\u00e9, jour apr\u00e8s jour, soir\u00e9e apr\u00e8s soir\u00e9e, jusqu'\u00e0 \u00e9puisement des cyberressources. Transform\u00e9 par ce que j'avais lu et visionn\u00e9, j'ai r\u00e9\u00e9crit mes textes. Essentiellement, je les ai simplifi\u00e9s. Le contenu ne devait pas \u00eatre saisi par les mots mais par le medium lui-m\u00eame. <<Medium is message>> dit un vieux dicton.\n\nComme dans le travail de Saul, ce qui faisait oeuvre dans mes textes, c'\u00e9tait la fa\u00e7on dont ceux-ci s'encodaient dans les supports num\u00e9riques pour \u00eatre largement diffus\u00e9s sur les r\u00e9seaux publics.\n\nRepli\u00e9 sur mon petit univers, je n'avais pas conscience de la menace qui pesait sur ma ville comme sur la capitale.";
      }
    
      public void draw() {
        background(0);
        println(frameRate);
        offset = offset + (mouseY - pmouseY);
        textSize(30);
        textAlign(CENTER, TOP);
        fill(255);
        pushMatrix();
        translate(0, offset);
        text(texte1, width*0.05f, height*0.05f, width*0.9f, height * 10);
        popMatrix();
      }
    }
    
  • edited October 2015
    • The example above "violates" 2 of my 3 "rejected" advises: 8-}
    • textSize() within draw() & text()'s "fixed" coordinates & dimensions.
    • I'd also add textAlign() to that list too. All fixed configurations should stay in setup()!
    • Since text() got fixed coords., just create a PGraphics of it in setup().
    • And use image() or set() to display it within draw().
  • the problem is that in my full sketch, I don't have this single page but many. I put each one in a separate void and text size and align are different in almost every page.

  • Surely the entire String doesn't fit on your screen. Couldn't you figure out what part of the String is visible, make a much shorter substring, and just display the substring?

  • Well, I'd rather not. I'd prefer to display my text with the scrolling I use. What I don't understand is that in Processing mode, this sketch runs at 60fps and in an Android device, it falls down at around 5 or 10 fps depending on the device...

  • A string is an array of characters and arrays makes fall your frame rate especially full of space like 3k that needs to be run index by index on a repeatable mode like inside the draw method. The pushMarix and popMatrix to make an illusion of movement, moving images, also decrease the framerate. Maybe the mobile device have less memory/processing hardware capabilities than the computer. You are using a default letter, there are more options.

  • Thanks Infsys,

    "3k" : what's that?

    "You are using a default letter, there are more options" :do you mean that using a PFont would be better?

  • 3k -> 3kilo -> 3 thousand; 3 thousand words full of characters. I modified your code to know what is happening. I add a constrain method so the limits are set. Just saying, not tested/tried, maybe setting this string to pdf, on a class, and translating it as an image can be an option. https://processing.org/reference/libraries/pdf/ <- The last part GoToLoop last two points are other options to try.

      String text;
      // For testing so I will not write threethousand words
      //----------------
      String text1 = "";
      //----------------
      float  offset;
      public void setup() {
        orientation(LANDSCAPE);
        //textAlign(CENTER, TOP);
        textSize(70);
        fill(255);
        text = "Hello, today it's sunny. Want to play basketball? Lets play a 21! Song in spanish is canción.";
        // For testing so I will not write threethousand words. 177 repeats * 17 words of my text = 3009 words.
        //-----------------------------
          for (int i = 0; i < 177; i++)
          text1 += (i+1) + ". " + text;
        //-----------------------------
      }
      void draw() {
        background(0);
        offset = offset + (mouseY - pmouseY);
        offset = constrain(offset, -(height*57) + height*0.9, height*0.05);
        pushMatrix();
        translate(0, offset);
        text(text1, width*0.5, height*0.05, width*0.5, height*57);
        popMatrix();
        text("Frame Rate: "+nf(frameRate, 1, 2), 0, height*0.05);
      }
    
  • Thanks Infsys,

    But the only thing I see is that you constrained the offset. That doesn't change anything about the performance...

    Putting the textesize, textalign or textfont in the setup (if we don't want to change them) makes it run hardly faster, the difference is scarce.

    Well, I think we can conclude that displaying text in Android mode consumes a lot of CPU if we don't want to create a bitmap or Pgraphics. What else?

  • edited October 2015 Answer ✓

    If you know Java, make this in Android Studio or Eclipse and try it there. You can import a processing project from there. I improve the code but in the instance you touch the screen the frame rate falls.

              String text;
              // For testing so I will not write threethousand words
              //----------------
              String text1 = "";
              //----------------
              float offset;
              void setup() {
                orientation(LANDSCAPE);
                background(0);
                fill(255);
                textSize(70);
                text = "Hello, today is sunny. Want to play basketball? Lets play a 21! Song in spanish is canción.";
                // For testing so I will not write threethousand words. 177 repeats * 17 words of my text = 3009 words.
                //-----------------------------
                  for (int i = 0; i < 177; i++)
                  text1 += (i+1) + ". " + text;
                //-----------------------------
                firstText(text1);
              }
              void draw() {
                if (mousePressed) {
                firstText(text1);
                }
              }
              // Maybe the huge text is not repeated every time the draw runs.
                void firstText(String text) {
                background(0);
                offset = offset + (mouseY - pmouseY);
                offset = constrain(offset, -(height*57) + height*0.9, height*0.05);
                pushMatrix();
                translate(0, offset);
                text(text, width*0.5, height*0.05, width*0.5, height*57);
                popMatrix();
                text("Frame Rate: "+nf(frameRate, 1, 2), 0, height*0.05);
                }
    
  • That's very smart infsys, I'm gonna try it!

Sign In or Register to comment.