[java] Efficiency question

Started by
5 comments, last by hapaboy 22 years, 3 months ago
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 =)
Advertisement
In this case, you should use the second one. A smart compiler will multiply things that never change during run time (like 3*3*2) during the compile, so your program would really do something like:

total = 18;
total = 36;

Of course, the actual speed difference here is virtually zero.
What *should* be going on if you have a semi-decent compiler is this:
  numb1 = 3*3;//Compiler would read/compile asnumb1 = 9;//Compiler should/may take this line out complete!total = 9*2;//Since it''s not doing anything...//Will calculate like thistotal = 9*4;//Using the numb1 variable.  


Your second example should be like so:
  //The compiler *should* remove this line completely!total=3*3*2;//Since it''s nto being used!total=3*3*4;//This line would get converted like so:total=36;  


The second version would be compiled better because while running, it would just be like doing this:
  int total;total = 36;  

Which sets the total to a number without ANY calculations.

Billy - BillyB@mrsnj.com.

ps. Some compilers aren''t smart enough to omit lines that do nothing, so they may stay in there.
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.
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 =)

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
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

This topic is closed to new replies.

Advertisement