Slowdown when looping through IntList()'s

Hi folks! I have been using IntList() to store an arbitrary amount of mouse coordinates. i chose intlist because it does not require the size to be declared in the beginning. I have been looping over these points to draw a graph using the following code:

void drawgraph(){
  //draw the visual rep of the positional points
  int buffer =  10;
  rect(0,height - graphHeight,width,graphHeight +20);
  if(posX.size() > 1){
    for(int i = 0; i < posX.size() -1; i++){
    float a = map(posX.get(i),maxleft,maxright,height-buffer, height - (graphHeight -buffer));
    float b = map(posX.get(i+1),maxleft,maxright,height-buffer, height - (graphHeight -buffer));
    float a1 = map(posY.get(i),maxup,maxdown,height - (graphHeight -buffer), height-buffer);
    float b1 = map(posY.get(i+1),maxup,maxdown,height - (graphHeight -buffer), height-buffer);



This works fine but it does seem to use a lot of CPU. the longer the list gets(and subsequently has to be displayed) the more of the cpu its hogging) I want to incorporate a third data steam into the mix and when i do its grinds to a halt. Is there a better way for me to accomplish this? I have been looking into using Pgraphics to speed things up which works great for static graphics but gets a bit complicated when things are relatively dynamic. Anly help would be much appreciated. Thanks, Danny.


  • Caching calculations in variables before a loop speeds things up a lil': *-:)

    // slowdown-when-looping-through-intlist-s#Item_1
    // 2017-Mar-30
    static final int BUFFER = 10;
    void drawGraph() {
      rect(0, height - graphHeight, width - 1, graphHeight - 1);
      final int len = posX.size();
      if (len < 2)  return;
      final int heiBuf = height - BUFFER;
      final int graBuf = height - graphHeight + BUFFER;
      final int[] locX = posX.values(), locY = posY.values();
      int i = 0, px = locX[0], py = locY[0];
      while (++i < len) {
        final int x = i + xLen - 1;
        final float a = map(px, maxLeft, maxRight, heiBuf, graBuf);
        final float b = map(px = locX[i], maxLeft, maxRight, heiBuf, graBuf);
        final float c = map(py, maxUp, maxDown, graBuf, heiBuf);
        final float d = map(py = locY[i], maxUp, maxDown, graBuf, heiBuf);
        line(x, a, x, b);
        line(x, c, x, d);
  • You could calculate store the 'mapped' mouse values in the IntList rather than recalculating then every frame.

  • edited March 2017

    Nice idea to store all mapped values, @quark.
    Every time a new coordinate pair is added, map() them in another parallel container as well.

  • Thanks for these. Yes it makes a lot of sense to do as many calculations before a loop as possible. Is an intList the best option to use? I have recollection that lists are a bit slower than plain old arrays but i chose them do to not having to specify their size at the beginning as you do with an array.

  • GoToLoop - im just trying to figure out what you have done in that code. in this part

    final int[] locX = posX.values(), locY = posY.values();
    int i = 0, px = locX[0], py = locY[0];

    Have you placed the values of the list into an array to speed things up? by using the .values method you seem to have done this without needing to use another loop...? Thanks.

  • If a container's length is fixed, regular arrays are indeed the most performant option for Java.
    You can simply declare an array variable at 1st.
    Then delay the array's instantiation until its final length is finally known. *-:)

  • That makes sense but one would still require the list to place the values into in the first place?Then one could move them into the array as you did with that slice of code i included in my last post if i understood that correctly?

  • edited March 2017

    How do you determine how many values you're gonna store in your current sketch?

    Just before the loop you use to store the values, create the array w/ the now known length, and assign it to its already declared global variable.

  • The loop is started and ended with a key press with an unknown amount of time in-between thus creating an unknown amount of data.these are currently added to my list using .append .

  • If your container got its length changed all the time, you should stick w/ IntList.

    Anyways, an extra tip: Given the x & y values are always accessed as a pair, it makes more sense & performance to store them together in the same container. :>

    Then you iterate over the unified container by 2 indices step instead of 1. :P

  • edited March 2017

    Yes thats in interesting thought! So the index for the x value would be n and the y at n +1?

  • Yup. All even indices are x and all odd indices are y. >-)

  • Nice! I will give that a shot!

Sign In or Register to comment.