Loading...
Logo
Processing Forum

performance issues

in Programming Questions  •  1 year ago  

hello all,

I have performance issues within a for-loop that lies in a recursion.

Can I just ask some questions?

I learned while searching for "performance" in the forum the compiler is pretty bright and makes good decisions.
Obviously, I declare everything outside loop and recursion to save it time.


1st question: Does the data type make a difference in terms of speed?

Let's say
  • PVector []
versus
  • int[]  // for x   (or float)
  • int[]  // for y
  • int[]  // for z


or say
  • PVector []
versus
  • MyOwnClass[]

Any difference?


2nd question: Does the usage of Strings and + need a lot of time?
  • I heard usage of + with String is not recommended - what does the merging with StringBuilder look like?


3rd question: Allocated memory?
  • When I make the memory usage higher in my IDE and compile, does the exe-file (the application) under Win 64 knows the higher memory usage? Or how can I enforce it? (or doesn't it matter since Win 7 provides memory dynamically?)

4th question: Are there other pitfalls?
  • What are other pit falls?

Thank you!

Greetings, Chrisir   


P.S.
I am referring to this beast, namely to tab Game2
(doesn't run in browser)
http://www.openprocessing.org/sketch/65645

Replies(13)

Hey,

without looking into your code yet.
1. It makes a difference, using three int-arrays is faster than using PVector, but i think it only matters if you have lots of iterations. I can't imagine, why there should be a differnce in performance between PVector and your own classes.

2. concat() is about twice as fast as combining strings with "+=".
But the differnce when using StringBuilder is huge. An example:
Copy code

  1. String message = "";
    int num = 10000;

    int t = millis();
    for (int i = 0; i< num ; i++) {
      message += "b";
    }
    println("'+=' : "+(millis()-t));

    message="";
    t = millis();
    for (int i = 0; i< num ; i++) {
      message = message.concat("b");
    }
    println("concat(): "+(millis()-t)); 

    t = millis();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i< num ; i++) {
      sb.append("b");
    }
    message = sb.toString();
    println("StringBuilder(): "+(millis()-t));

No ideas to 3. and 4. at the moment, but maybe i'll have a look into your example later.

I was able to do the calculations now without any usage of String. And this gave a huge improvement on performance.

Thank you very much for pointing me to the fact that String can be problematic!

I am doing now everything via int[][][] and reduced that again to byte[][][].

Thanks!

Greetings, Chrisir 



IIRC, the memory setting set in the PDE is exported, in the args.txt file.
That's great, thanks you to both!

In fact I don't need String at all, it could be all done via int[][][].

Anyway, it's a shame that arrayCopy or isEqual doesn't work for 3D arrays - I need to for-loop there as well.

Is % and join considered to be fast?

Thanks again!

Greetings, Chrisir       

hello,

I am still struggling with this....

I need to do one thing fast :

  • compare two 3D arrays

is there a better was than to do a triple for-loop?

I was thinking of use concat to have the data of the 3D array in a 1D array and then use
join to have it as a string and then use .equals on the string - thus no need for a for-loop at all.
But how can I use concat to have the data of the 3D array in a 1D array?

Thanks a lot!

Chrisir


Not sure if that is the fastest solution, but have you tried Arrays.deepEquals() ?
Copy code
  1. int[][][] a1 = new int[4][4][4];
    int[][][] a2 = new int[4][4][4];

    void setup() {
      for (int i = 0; i< a1.length; i++) {
        for (int j = 0; j< a1[0].length; j++) {
          for (int k = 0; k< a1[0][0].length; k++) {
            a1[i][j][k] = 3;
            a2[i][j][k] = 3;
          }
        }
      }
      println(Arrays.deepEquals(a1, a2));
    }

That was very helpful!

Thanks a lot!




hello,

sorry,

Another performance question from this program :

I'm using my own function arrayCopy3D (see below) with 2 for-loops to copy a 3D-Array.
To use = doesn't work because it copys only the adress in the memory. Not ok.
Is there a faster way such as deepCopy?

Copy code
  1. int[][][] arrayCopy3D ( int[][][] src ) 
  2. {
  3.   int[][][] dest;
  4.   dest=new int [intConstEnd][intConstEnd][intConstEnd];
  5.   for (int j=0; j<src.length; j++) {
  6.     for (int i=0; i<src.length; i++) {
  7.       arraycopy(src[j][i], dest[j][i]);
  8.     } // for
  9.   } // for
  10.   return dest;
  11. } // func


Thanks!


Greetings,

Chrisir    




Hello,

another question from working on this program:

I use several times pegs [i].x.

Would it be faster if I said

Copy code
  1. int x1= pegs [i].x;

and then only use x1 further on?

Thanks, Chrisir  



Well, there might be a little performance gain, in theory... The difference might be hardly perceptible in a real sketch. And the Java compiler, or even the JIT compiler can even reduce the difference. Although only measurement can really tell. If you are passionate about these questions, look for the Caliper library, it can help you to answer these.

Note that another reason for writing the expression above is... it helps readability and can reduce typing!

Thank you very much!


Re: performance issues

9 months ago
Hi,
I'm also interested in speed of PVector class because I use it for many-body simulation such as molecular dynamics ( http://en.wikipedia.org/wiki/Molecular_dynamics) or mechanics of truss ( http://en.wikipedia.org/wiki/Truss )

I just made some test of performance where i propagate 10^7 - 10^8 iteration of dynamics of 3D harmonic oscillator by different methods: 
  1. "float"  - everything is computed explicitly in floats
  2. "PVector1" - uning PVectors, global temporary PVector "temp" is used to avoid creation of new objects
  3. "PVector2" - using PVectors in naive way ( new objects are crated in operations like PVector.mult(k,x)  )
  4. "MyVector" - I made class similar to PVector however with operations like addmult and setmult to avoid creation of new objects or copying to global temporary object
Result of the test:
Copy code
  1.   time taken by 10^7 iterations
  2.   float           219.0 ms
  3.   MyVector   313.0 ms
  4.   PVector1     437.0 ms
  5.   PVector2   719.0 ms

  6.  time taken by 10^8
  7.   float            2187.0 ms  
  8.   MyVector    3079.0 ms
  9.   PVector1     4516.0 ms
  10.   PVector2    7406.0 ms
Code of the methods:

"float" method 
Copy code
  1.   void move( ){
  2.     vx-=dt*fx/m; vy-=dt*fy/m; vz-=dt*fz/m;
  3.     x+=vx*dt;  y+=vy*dt; z+=vz*dt;
  4.   }
  5.   void force( ){
  6.     fx=kx*x; fy=ky*y; fz=kz*z;
  7.   }

"MyVector" method
Copy code
  1.   void move( ){
  2.     v.addmult( f, -dt/m );
  3.     x.addmult( v,  dt   ); 
  4.   }
  5.   void force(){
  6.     f.setmult( k, x );
  7.   }
"PVector1" method
Copy code
  1.   void move( ){
  2.     temp.set(f);  temp.mult(-dt/m);   v.add(temp);
  3.     temp.set(v);  temp.mult(dt);      x.add(temp); 
  4.   }
  5.   void force(){
  6.     f.set(x); f.mult(k);
  7.   }
  "PVector2" method
Copy code
  1.  void move( ){
  2.     v.add( PVector.mult( f, -dt/m ) );
  3.     x.add( PVector.mult(v,dt) ); 
  4.   }
  5.   void force(){
  6.     f.set( PVector.mult(k,x)  );
  7.   }

My Conclusion: 
  • The difference is smaller than I thought, the most stupid way (PVector2) is just 3-times slower than the fastest "float" version
  • Would be nice to have composed operations as "addmult" in PVector class, not just because of speed, but also because of readability and simplicity of code
  • If PVector would use fused multiply–add (FMA) or fused multiply–accumulate (FMAC) and SSE vector operations it would be probably even faster than "float" method

Re: performance issues

9 months ago
This thread is full of interest in every post. Thanks to everyone!

Ale · http://60rpm.tv/i