Compiler Optimization

L

Luc The Perverse

Hello. This is more a question of curiousity than actually needing a
performance increase.

I was wondering if the Java compiler (I am using Sun's j2sdk 1.4.2_10) will
change Math.PI * 2.0 into a constant in bytecode, or should I make a final
double above my function call?
 
S

Stefan Ram

Luc The Perverse said:
I was wondering if the Java compiler (I am using Sun's j2sdk 1.4.2_10) will
change Math.PI * 2.0 into a constant in bytecode, or should I make a final
double above my function call?

You can use

javap -c <classname>

to see the bytecode.
 
N

NullBock

Equations solely consisting of compile-time constants are typically
solved before being compiled to byte code. This is also true of
strings, so :

String s = "I want " + ((23 + 7) / 3) + " big macs";

would be stored in byte code as "I want 10 big macs".

Walter Gildersleeve
Freiburg, Germany

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
 
L

Luc The Perverse

NullBock said:
Equations solely consisting of compile-time constants are typically
solved before being compiled to byte code. This is also true of
strings, so :

String s = "I want " + ((23 + 7) / 3) + " big macs";

would be stored in byte code as "I want 10 big macs".


Oh that is even smarter than I thought :)

Alright cool thanks.
 
T

Thomas Hawtin

Luc said:
Hello. This is more a question of curiousity than actually needing a
performance increase.

I was wondering if the Java compiler (I am using Sun's j2sdk 1.4.2_10) will
change Math.PI * 2.0 into a constant in bytecode, or should I make a final
double above my function call?

Should the value of PI change, then you will need to recompile your code.

Looking at the bytecode is not a particularly good way of determining
performance. The real optimisations happen at runtime, once your code
has been run 10,000 times. So when performance counts, benchmark your
actual code in a close to real life situation.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top