We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionGeneral Discussion,  Status › Processing/Java Optimisation Techniques
Page Index Toggle Pages: 1
Processing/Java Optimisation Techniques (Read 1376 times)
Processing/Java Optimisation Techniques
Oct 29th, 2008, 9:59pm
 
Hi Guys,

It's been quite a while since I posted anything on here. Last time I think I asked some OpenGL questions or something. Anyways I've been trying to find ways to make processing applets load and act closer to flash in the browser because I want to use it for some web development I'm doing. If you like you can check out the current version of my sketch here: http://24.17.252.68/test
(hosted from apache on my home laptop so expect slow loading times)

So, my first question is about what everyone's current techniques are for Java optimization. I think the biggest issue I'm finding is that any sketches I do tend to get small hitches in them (I believe this is from the garbage collector running)

The easiest but least desirable solution is to lower the set frame rate of the sketches so that there is more time for the garbage collector to run between frames. This smooths out the time between frames but obviously lowers the frame rate.

What I'm wondering is if there would be any use in writing my own pseudo memory manager because I do have a lot of function level declaration of variables for calculations. preferably these would not have to be allocated and freed every time the functions are called because in total they do not amount to a very large amount of memory and they're always needed. There is no recursion so I should not need more than one copy of each, and they're always initialized. Is it better with Java to always declare all your variables at the start of the function?

I've also avoided as much allocation and loading from the setup() section of the sketch so that the application can start rendering faster. Then I render the frame with whatever the first look i want it to be (in this case I generated a gradient and render that on the first frame so no data loading is required, just the .jar file) then I start remote loading any images I need from the server instead of putting them in the data folder to reduce the jar size. Are there other ways of reducing the jar size as well? It would be nice if there could be a main jar that dynamicly could load and unload other jar files from the server as needed however that's probably more complicated than what is required at this point.

I'm also thinking of spawning a background thread to do the loading of the images as well as creating some kind of interface between javascript and the applet to make the applet notify the javascript when to update the page to display the applet after it is finished starting the JVM and loading the applet.

I'd be happy to hear from anyone as far as what they do to make thier 3D apps run more smoothly! (I'm not using OpenGL at this point for compatibility)


Jack Kern

Re: Processing/Java Optimisation Techniques
Reply #1 - Nov 1st, 2008, 6:14pm
 
if you are using object oriented programming languages you don´t think about optimisation ..
Re: Processing/Java Optimisation Techniques
Reply #2 - Nov 1st, 2008, 7:52pm
 
coldarchon wrote on Nov 1st, 2008, 6:14pm:
if you are using object oriented programming languages you don´t think about optimisation ..


You might not need to - but to push the limits of any language it's important to know how to use the language well. A good C++ programmer can make their code just as fast its C counter part all while making the code easier to understand in general.

I may take some time and write some profiling code to figure out the answers to these questions myself. I will be happy to post the code and my results here when I finish.

Jack
Re: Processing/Java Optimisation Techniques
Reply #3 - Nov 2nd, 2008, 10:41pm
 
I don´t want to sound rude, but if you think you can make oo code as fast as procedural code and more readable at the same time - you have no idea what you are talking about ..
Re: Processing/Java Optimisation Techniques
Reply #4 - Nov 3rd, 2008, 12:23am
 
coldarchon wrote on Nov 2nd, 2008, 10:41pm:
I don´t want to sound rude, but if you think you can make oo code as fast as procedural code and more readable at the same time - you have no idea what you are talking about ..


For most programmers I would probably agree with you. If you try use classes all over the place and you stick to strict guidelines of coding in an object oriented language, it will be moderately slower than it's procedural counterpart. I currently work as tools engineer for Monolith Productions (Shogo / NOLF / Fear / Condemned franchises) and I work on our World Editor in close relationship with all the engine programmers. We do most of our code (including engine & game code) in C++ with some intermittent assembly only in the tightest loops. Avoiding certain aspects of C++ (like polymorphism) where necessary for speed and memory savings. I find it a bit offensive that you would say I don't know what I am talking about without even knowing me. That is a great assumption you made about me. However, that is besides the point of my thread.

To address your statement - in my experience using object oriented code is not what makes an application run fast or slow, more often it's the way code is implemented. A functional (or procedural as you termed it) language like C can be faster for the average programmer because the syntax defines a simpler tool set. This affects both the amount of machine code required to implement the tools that are provided, and even more importantly programmers are less likely to use the language wrong when there is less to work with in general.

With a language like C++ or Java, (ignoring the fact that Java is compiled to a byte code that is interpreted by a virtual machine) the language itself provides a much more robust tool set to write code that can help or hurt the programmer depending on how it is used. Often, all that is required to create the fastest code is a better understanding of how the compiler and computers work in general. An example would be understanding how to make your allocations correctly fall along each platforms cache boundaries so memory can be most optimally fetched into working memory on the CPU. Cache misses are often one of the greatest bottlenecks in high performance applications. Current generations of object oriented compilers generate VERY fast code in most circumstances.

I guess I was just looking for more insight into how Java does it's memory management to see if I could tell the JVM that it doesn't need to worry about checking to see if it should free up memory for me. In C++ it's better to allocate as much up front as you can as to avoid heap allocations. Heap allocations are much slower since you have to rely on the operating system to provide you with the correct sized chunk of memory. However, if you allocate a large chunk like an array upfront and track your usage yourself, you avoid that performance hit of searching for memory - at the cost of a bit more code to manage your memory in your application.

Anyways if anyone could even just point me in the direction of a good book on writing high performance applications in Java, I would be grateful and happy to share my learnings.

Jack Kern
Re: Processing/Java Optimisation Techniques
Reply #5 - Nov 3rd, 2008, 1:35am
 
Quote:
if you are using object oriented programming languages you don´t think about optimisation ..


I don't want to sound rude, but thats bullcrap. Of course a well written java app will perform much better than a badly written java app, and that is what the OP is asking. Saying that all hope is lost because its an OO language so to not think about optimization is the craziest thing I've heard. There are plenty of java (& processing) apps out there that perform perfectly at the level required. Of course they could perform faster if the same algorithms were coded in C. But that doesn't mean don't bother writing efficient java code!

E.g., some VERY basic examples off the top of my head that might be of some use to anyone interested in optimizing their code:

using vertex buffers is a lot faster than calling glvertex thousands of times (and so can be the old school display lists)

To see if two points are within a certain distance of each other, its quicker to check the square of the distance as opposed to the actual distance (if(p1.x*p1.x+p2.x+p2.y < dist*dist) instead of if(sqrt(p1.x*p1.x+p2.x+p2.y < dist) );

Divisions are slow, if you have loops with millions of divisions per second, try and cache the reciprocal of the divisor and replace them with multiplications (e.g. *0.2 instead of /5);

In opengl, changing the state of something has overheads, even if the state isn't changing (i.e. putting glEnable(GL_BLEND) in a for loop and enabling it hundreds of times a frame when it can be enabled once, then looped, will improve performance);

Similar to above, binding a texture once, then drawing thousands of particles will be much faster than binding, drawing, unbinding for each particle;

String concatenations are slow, so try not to do them in massive loops, use StringBuffer if you must;

in opengl, having depth testing turned ON can improve graphic performance drastically if you are drawing a lot to the screen, and a lot of what is being drawn is supposed to be behind other stuff

Try to avoid resizing arrays a lot, it can be a lot quicker to set them to the max size you think you'll need up front,

etc.. the list is huge and goes on... and none of the above have big impact on readability. Of course C can perform faster than java... but that doesn't mean you shouldn't think about optimization when using OO!
Re: Processing/Java Optimisation Techniques
Reply #6 - Nov 3rd, 2008, 3:54am
 
Thanks Memo for that well thought out post. Those examples are all very good, classic optimizations...

I'd add in early outs whenever possible, so an example would be if you're checking to see if a ray intersects a sphere - which involves calculating a fairly large equation to calculate the time(s)of intersection along the ray, then you can early out if you find out that the value under the square root in that equation is less than zero meaning the result is an imaginary number and the intersection will not happen.

Here's an example test I wrote to check calculation speeds. You'll notice the fastest one (At least on my machine) is the last test I wrote, where I'm passing a simple object into the function instead of multiple objects. My guess is either 1 all the float params are passed by value into the function because they're 4 bytes, or else each has it's own reference that's being passed which makes it slower.

Anyways if you guys could test this too and add your own tests it would be helpful!

I'm more concerned with Java specific optimizations over graphical ones, as I'm pretty familiar with how to make rendering fairly optimal but I'm less familiar with the workings of Java itself.

Here's my code:
Quote:

float polynom1( float x, float a, float b, float c, float d, float e )
{
 return a*x*x*x*x + b*x*x*x + c*x*x + d*x + e;
}

float polynom2( float x, float a, float b, float c, float d, float e )
{
 float x2 = x  * x;
 float x3 = x2 * x;
 float x4 = x3 * x;
 return a*x4 + b*x3 + c*x2 + d*x + e;
}

class SimplePoly
{
 public float a, b, c, d, e;
};

float polynom3( float x, SimplePoly s )
{
 float x2 = x  * x;
 float x3 = x2 * x;
 float x4 = x3 * x;
 return s.a*x4 + s.b*x3 + s.c*x2 + s.d*x + s.e;
}



void setup()
{
 delay(2000); // Alow for java to fully start up
 
 int t1, t2;
 final int   count = 50000000;
 SimplePoly  p     = new SimplePoly();
             p.a   = 0.1f;
             p.b   = 2.5f;
             p.c   = 1.0f;
             p.d   = 0.5f;
             p.e   = 1.0f;
 
 // Start the timer
 t1 = millis();
 
 double val = 0.0;
 for( int i=0; i<count; ++i )
 {
   val += polynom1( (float)i, p.a, p.b, p.c, p.d, p.e );
 }
 
 // End the timer
 t2 = millis();
 
 println( "--------------------------------------" );
 println( "Polynomial test 1:" );  
 println( "result: " + val );
 println( "time taken (miliseconds): " + (t2-t1) );

 // Start the timer
 t1 = millis();  
 
 val = 0.0;
 for( int i=0; i<count; ++i )
 {
   val += polynom2( (float)i, p.a, p.b, p.c, p.d, p.e );
 }
 
 // End the timer
 t2 = millis();  
 
 println( "--------------------------------------" );
 println( "Polynomial test 2:" );
 println( "result: " + val );
 println( "time taken (miliseconds): " + (t2-t1) );
 
   // Start the timer
 t1 = millis();  
 
 val = 0.0;
 for( int i=0; i<count; ++i )
 {
   val += polynom3( (float)i, p );
 }
 
 // End the timer
 t2 = millis();  
 
 println( "--------------------------------------" );
 println( "Polynomial test 3:" );
 println( "result: " + val );
 println( "time taken (miliseconds): " + (t2-t1) );
}



Re: Processing/Java Optimisation Techniques
Reply #7 - Nov 3rd, 2008, 3:56am
 
And these were my results:
Code:

--------------------------------------
Polynomial test 1:
result: 6.25000370548578E36
time taken (miliseconds): 1531

--------------------------------------
Polynomial test 2:
result: 6.250003691951651E36
time taken (miliseconds): 1410

--------------------------------------
Polynomial test 3:
result: 6.250003691951651E36
time taken (miliseconds): 1373

Re: Processing/Java Optimisation Techniques
Reply #8 - Nov 3rd, 2008, 9:07am
 
we need to specify parameters to continue this discussion, because if you talk about java optimization and your given examples are basic algorithm optimization or external libraries debugging we will hardly find helpful answers.

a good book you are asking for would be o´reilly´s java extreme programming cookbook.

personally I found anything about hibernate well thought out and well structured.
Re: Processing/Java Optimisation Techniques
Reply #9 - Nov 3rd, 2008, 6:21pm
 
Just found this while looking for more Java specifics. This was enlightening to me as it very clearly describes that Java does not actually pass by reference in any circumstances - it merely passes pointers to objects that it calls references to avoid confusing them with how pointers work in C/C++.

This could be counter-intuitive to most Java folks, but coming from a C/C++ background this helped to clarify what's going on behind the scenes for me because Java references are VERY different from C++ references.

http://javadude.com/articles/passbyvalue.htm
Re: Processing/Java Optimisation Techniques
Reply #10 - Nov 4th, 2008, 3:37am
 
just my 2cents that i read somewhere...
in java, every object that you create is allocated in the heap (except for primitive types like float, int, etc). so yo could try to write code that minimizes the amount of new objects being created, for example inside loops.

so for example this:
Code:

Vec3D someVec = new Vec3D();
for(int i=0; i<1000; i++){
// do something with vector...
}


is faster than this:
Code:

for(int i=0; i<1000; i++){
Vec3D someVec = new Vec3D();
//do something with vector...
}
Page Index Toggle Pages: 1