Ok, I want some esoteric knowledge on optimizing Java. I''m sure there are some out there that have run tests to see which code is faster, stuff you can''t read in books. Please share you knowledge.
So I wonder:
* Does final classes and methods speed up code?
* Does protected vs. public variables speed up code?
* How much faster is a Vector that has a defined capacity? For instace is a Array(1000000) better than Vector ()?
* JRE 1.3 uses hotspots to optimize code, are there any other compilers out there that are faster in runtime?
* Loading a new class seems to eat alot of time. Is it faster to clear all the values in that object instead of creating a new one in for instance a large loop?
You get the picture, any replies are welcome.
[java] Optimizing Java - Java Pros look here!
>> * Does final classes and methods speed up code?
Yes, but not much. It is bad style to overdo the use of final. Don''t make you program methods/classes final before you are complete with the program.
The first law of optimization:
"Never optimize something that doesn''t work!"
>> * Does protected vs. public variables speed up code?
No.
>> How much faster is a Vector that has a defined capacity? For instace is a Array(1000000) better than Vector ()?
If you will actually put all those elements in it then it will
be faster, but if you are not using all the space your data will use a lot of more space. This may mean that you will waste CPU cache space and thereby make your program slower. So the anser is: If you know the size you need then preallocating is faster. If you preallocate way to much it may actually be slower.
>> * JRE 1.3 uses hotspots to optimize code, are there any other compilers out there that are faster in runtime?
Use a native code compiler such a GNU GCC Java front-end, WebGain Visual Cafe 4.0 Expert Edition or Visual Age for Java Enterprise Edition.
Use JNI to write parts of your program in C++ that needs very high performance - such as tight inner loops in the game engine.
>> * Loading a new class seems to eat alot of time.
If you want to avoid loading a class during the program works you can load it at load time by making a reference to the class. This uses more time during loading but speeds up execution time.
>> * Is it faster to clear all the values in that object instead of creating a new one in for instance a large loop?
Yes. New''ing is quite slow. Always clear fields instead (or just overwrite them with the new values) of making a new one. This is espcially important for small objects.
Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Ok, thanks felonius.
How does JNI work? Are there any drawbacks to this? Can you create a instance of a C++ object directly in Java and then operate on its methods? You counted up a few other alternative compilers to JRE1.3, but how much better are they?
How does JNI work? Are there any drawbacks to this? Can you create a instance of a C++ object directly in Java and then operate on its methods? You counted up a few other alternative compilers to JRE1.3, but how much better are they?
About JNI, I suggest you start reading the JNI tutorial either with Thinking in Java or at Suns site. Read those I you will understand what it can do.
And about those other compilers they are a lot faster, espcially if you do Swing. Swing applications often become a factor of 5 faster. Other applications become a lot faster too.
The few informal tests I have made shows optimed native code to be 10% faster to 50% slower than optimized C++ code. This is at least as double as fast as Java 1.3 (I haven''t measured 1.3 with a clock though) When running apps with 1.3 in comparison I see significant speed differences.
Remember, though, native compilation is not available for applets and the code cannot be moved to other platforms after they are compiled (the source code can however). Regular exe files are generated just as for C++.
Using JNI is quite possible to do high performance games. Take for instance the game Vampire - The masquarade. It uses java for the control code and C++ for those parts that needs to be high performance.
Jacob Marner
And about those other compilers they are a lot faster, espcially if you do Swing. Swing applications often become a factor of 5 faster. Other applications become a lot faster too.
The few informal tests I have made shows optimed native code to be 10% faster to 50% slower than optimized C++ code. This is at least as double as fast as Java 1.3 (I haven''t measured 1.3 with a clock though) When running apps with 1.3 in comparison I see significant speed differences.
Remember, though, native compilation is not available for applets and the code cannot be moved to other platforms after they are compiled (the source code can however). Regular exe files are generated just as for C++.
Using JNI is quite possible to do high performance games. Take for instance the game Vampire - The masquarade. It uses java for the control code and C++ for those parts that needs to be high performance.
Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Hello;
Here is a link that will give you a good start on JNI. Also check out the other articles that are also about JNI at Java World (where the link to the article above goes too ).
JP.
=============================
a wise man once said....
=============================
www.thejpsystem.com
Here is a link that will give you a good start on JNI. Also check out the other articles that are also about JNI at Java World (where the link to the article above goes too ).
JP.
=============================
a wise man once said....
=============================
www.thejpsystem.com
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
Ok so about these other compilers that do native compilation which are for free? I read somewhere that Symantecs compilator does not compile 1.3
Only the GCC front-end are for free. For windows you can find it as part of the free Cygnus-win32 package. This oly supports Java 1.1 and does not support inner classes. Furthermore, under Windows the final programs require cygnus-win32 to be installed on the target machine. This is only under Windows. Under Linux or some other Unix you will not need to do this.
The Webgain Visual Cafe (Webgain bought it from Symantec earlier this year) only supports Java 1.2 in the newest version, but for the sake of speed I can live with this. The greatest difference between Java 1.2 and 1.3 is the integrated hotspot VM (as far I know - enlighten me if I am wrong) and you won''t be using that anyway so I personally don''t care that it lacks 1.3 support.
About IBM Visual Age, I don''t know, but we warned, it is quite expensive. My local dealer wants $3000 for it so it is out of the question for most hobbyists.
Jacob Marner
The Webgain Visual Cafe (Webgain bought it from Symantec earlier this year) only supports Java 1.2 in the newest version, but for the sake of speed I can live with this. The greatest difference between Java 1.2 and 1.3 is the integrated hotspot VM (as far I know - enlighten me if I am wrong) and you won''t be using that anyway so I personally don''t care that it lacks 1.3 support.
About IBM Visual Age, I don''t know, but we warned, it is quite expensive. My local dealer wants $3000 for it so it is out of the question for most hobbyists.
Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
The Webgain Visual Cafe (Webgain bought it from Symantec earlier this year) only supports Java 1.2 in the newest
version, but for the sake of speed I can live with this.
Ok and you just compile and it works, or are there alot of problems and hours of fixing etc.
version, but for the sake of speed I can live with this.
Ok and you just compile and it works, or are there alot of problems and hours of fixing etc.
Well, the current version 4.0a is still somewhat buggy, but when you figure out how to avoid those bugs programs can be compiled both to an exe and to normal class files *without change*. You just change a parameter in IDE or on the command line.
WebGain has just released a version 4.1 which they are sending to all regsitered users but I haven''t recieved it yet (the 6 weeks delievery time for this free patch hasn''t elapsed yet) but it is primarily a bug fix release so I hope most of the problems are fixed in that.
On by the way, if you are student you can get Webgain Visual Cafe for about third of the normal price.
However it is not as simple as just making an exe a then running.
The run time system must be installed on the target machine and there is an installer included with Visual Cafe that does this for you.
Futhermore, Visual Cafe support the creation of DLLs written in Java. A Java DLL is jsut a set of classes put into a DLL. For other native Java programs to access this the DLL must be registered in the regsitry. A command line program is included for doing this and this must also be run during installation on the target machine.
A Java DLL has the follwing advantages:
* If several programs use the same classes they only need to be stored once on the hard disk.
* During development the files that make of the classes from DLLs does not have to be part of your main exe project file. This cleans up the project folder a lot in the IDE and is reason enough to use this feature.
* If several program use the same classes you are sure that the most current version is always used.
All this may sound like a lot of fuss, but if you use InstallShield or some other install program then it is quite easy really, but you just can''t expect people to be able to unzip your exe from the net and just run it.
Jacob Marner
WebGain has just released a version 4.1 which they are sending to all regsitered users but I haven''t recieved it yet (the 6 weeks delievery time for this free patch hasn''t elapsed yet) but it is primarily a bug fix release so I hope most of the problems are fixed in that.
On by the way, if you are student you can get Webgain Visual Cafe for about third of the normal price.
However it is not as simple as just making an exe a then running.
The run time system must be installed on the target machine and there is an installer included with Visual Cafe that does this for you.
Futhermore, Visual Cafe support the creation of DLLs written in Java. A Java DLL is jsut a set of classes put into a DLL. For other native Java programs to access this the DLL must be registered in the regsitry. A command line program is included for doing this and this must also be run during installation on the target machine.
A Java DLL has the follwing advantages:
* If several programs use the same classes they only need to be stored once on the hard disk.
* During development the files that make of the classes from DLLs does not have to be part of your main exe project file. This cleans up the project folder a lot in the IDE and is reason enough to use this feature.
* If several program use the same classes you are sure that the most current version is always used.
All this may sound like a lot of fuss, but if you use InstallShield or some other install program then it is quite easy really, but you just can''t expect people to be able to unzip your exe from the net and just run it.
Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement