Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

performance - java compiler optimization

Is the Java compiler smart enough to optimize loop below, by extracting the

Double average = new Double( totalTime / callCount ); 

out of the for loop?

public double computeSD( Set values, int callCount, long totalTime ) {
  double diffs = 0.0d; 
  for( Iterator i=values.iterator(); i.hasNext(); ) {
    double value = ( ( Double )i.next() ).doubleValue(); 
    Double average = new Double( totalTime / callCount ); 
    diffs += ( value – average.doubleValue() ) * ( value – average.doubleValue() );
  } 
  double variance = diffs / callCount;
  return Math.sqrt( variance );
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Nothing prevents the bytecode compiler (java->bytecode) from performing optimizations. When I worked at Symantec, and they did a Java IDE, the compiler write did look into putting some optimizations into our compiler but said nobody (in the outside world) seemed to be interested and the focus was on the Just In Time (JIT) compiler that is roughly the same as HotSpot in modern Sun VMs.

There is nothing that prevents a bytecode compiler from performing optimizations, but I am not aware of any that do so. There is huge focus on runtime optimizations, but those are pretty much hidden at runtime.

So, the source->bytecode compiler probably does not optimize it but the VM probably does. If you are on something like an Android then it probably performs no runtime optimization.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...