Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# [java] Efficiency question

This topic is 6127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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 =)

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632988
• Total Posts
3009738
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!