Skip to main content
gamedev.net
🔒 Locked

[java] Efficiency question

Started by hapaboy 5 replies 650+ views
Original Post
hapaboy
hapaboy
Im pretty new to Java programming and Im trying to think up of more efficient programming techniques. What Id like to know is what is actually going on behind something such as this: int total, numb1; numb1 = 3*3; total = numb1 * 2; total = numb1 * 4; as opposed to this: int total; total = 3*3*2; total = 3*3*4; What Im assuming is that in the first example, 9 is calculated once and stored in main memory for repeated use. In the second example, 9 is calculated multiple times. So that the first example will use up more RAM and the second will use up more processor power. Am I correct in those assumptions? If I am not can someone explain to me what is actually going on and which would be better to use for efficiency? Thanks to all who reply =)
snowmoon
snowmoon
The compilers are quite smart. Getting things coded in the first place is hard. Program with the way that it should work and then decide after profiling your code if you need to make it faster with tricks. Don''t start with optimizations, it''s the surest way to spin your wheels forever.
hapaboy
hapaboy
Thanks for the great replies everyone!!

Im starting to understand what is actually going on but I do have another question...actually just correct me if this is wrong, I like to make sure that I have the right idea:

From what I''m getting, good compilers will do many of the one time operations for you such as total = 3*3*4, before even converting it to machine code. They will even go as far as excluding lines that are not used for anything such as my first
total = 3*3*2 line. But they will not do automatic calculations including variables but will simply convert the variable names into the actual constants. Those calculations will then be taken care of in real time. But then, its all depending on how the compiler is written in the first place.

O and snowmoon, thanks for the tip =). I know what you mean and I admit I do have a problem with trying to make things efficient which is ridiculous considering the size of the applications that I have coded thus far =P. Im trying to fight the urge hehe. Despite this, I think its a good idea for me to know whats going on behind the scenes.


Please let me know if my above explanation is accurate.

Thanks again for all your help =)

JavaJohan
JavaJohan
Hi!

First of all, since you have an example, try to decompile the java code. There are a lot of them decompilers out there on the net. I remember one called JAD. There is also the javap tool included with Sun''s JDK, which you can use.

Second; there is a great way to optimize your code. Use the Jopt (Java OPTimizer). You can find it at;
http://www-i2.informatik.rwth-aachen.de/~markusj/jopt/index.html

Regards
Johan
Johan Karlsson
JavaJohan
JavaJohan
Hi!

First of all, since you have an example, try to decompile the java code. There are a lot of them decompilers out there on the net. I remember one called JAD. There is also the javap tool included with Sun''s JDK, which you can use.

Second; there is a great way to optimize your code. Use the Jopt (Java OPTimizer). You can find it at;
http://www-i2.informatik.rwth-aachen.de/~markusj/jopt/index.html

Regards
Johan
Johan Karlsson

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.