Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>JAVA基础>>java编译器对于循环进行了真正的编译优化吗?
java编译器对于循环进行了真正的编译优化吗?
2007-04-13   来源:www.javaresearch.org  作者:未知

此文章只是用来在java编译器进行循环化方面的验证,本文是有感而发,并希望求得大家的分析结果,这里引用了一位网友的话(没有人身攻击的色彩):
///////////////////////////
对于Vector v=new Vector()若v中包含了10000个对象,我们进行一个loop如下
for(int I=0; I<v.size(); I++) { System.out.println("testing..."); }
大家一定会用如下的写法吧 
int size = v.size(); for(int I=0; I<size; I++) { System.out.println("testing..."); }
///////////////////////////
但有些网友说(只是为了说明问题而已,无攻击色彩):
不能用完全解释语言过程来理解Java,v.size()在循环中并没有被调用10000次,因为,编译的循环优化会对其进行优化,
因此,实际运行时只运行一次,和用初始化v.size()的程序效率一样。这是很早的编译优化机制。 
大家肯定这种说法正确吗?我不太赞同,很简单地可以用一个test作难证:
一个类Vector的类TetstObj.java
  1. public class TestObj {
  2.   int count=0;
  3.   public TestObj() {
  4.     count+=5;
  5.   }
  6.   public int size(){
  7.     System.out.println("size="+count);
  8.     return count;
  9.   }
  10. }

测试类:test.java
  1. public  class test{
  2.   public test(){}
  3.   public static void main(String[] args){
  4.     TestObj similarVector=new TestObj();
  5.     for(int i=1;i<similarVector.size();i++){
  6.       //do something
  7.     }
  8.   }
  9. }

结果还是5个size=1的打印结果.
size=5
size=5
size=5
size=5
size=5
大家也可以分析一下java.util.Vector这个类(下面摘录Vector的部分源码)
例如只以一个insert对象而言:
  1. /**
  2.      * Returns the number of components in this vector.
  3.      *
  4.      * @return  the number of components in this vector.
  5.      */
  6.  protected int elementCount;
  7.  /**
  8.      * Inserts the specified object as a component in this vector at the 
  9.      * specified <code>index</code>. Each component in this vector with 
  10.      * an index greater or equal to the specified <code>index</code> is 
  11.      * shifted upward to have an index one greater than the value it had 
  12.      * previously. <p>
  13.      *
  14.      * The index must be a value greater than or equal to <code>0</code> 
  15.      * and less than or equal to the current size of the vector. (If the
  16.      * index is equal to the current size of the vector, the new element
  17.      * is appended to the Vector.)<p>
  18.      *
  19.      * This method is identical in functionality to the add(Object, int) method
  20.      * (which is part of the List interface). Note that the add method reverses
  21.      * the order of the parameters, to more closely match array usage.
  22.      *
  23.      * @param      obj     the component to insert.
  24.      * @param      index   where to insert the new component.
  25.      * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
  26.      * @see        #size()
  27.      * @see       #add(int, Object)
  28.      * @see       List
  29.      */
  30.  public synchronized void insertElementAt(Object obj, int index) {
  31.     modCount++;
  32.     if (index >= elementCount + 1) {
  33.         throw new ArrayIndexOutOfBoundsException(index
  34.                              + " > " + elementCount);
  35.     }
  36.     ensureCapacityHelper(elementCount + 1);
  37.     System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
  38.     elementData[index] = obj;
  39.     elementCount++;
  40.     }
  41.     /**
  42.      * Returns the number of components in this vector.
  43.      *
  44.      * @return  the number of components in this vector.
  45.      */
  46.    public synchronized int size() {
  47.     return elementCount;
  48.    }
  49.  

这象上面的similarVector对象一样,v.size()会每次调用Vector中的size()方法,10000次的调用一次也不会少吧!!
所以就我自身来说我不会相信java的对循环的编译优化,你呢??有何见解,请给出你的想法!大家也可以评论一些其它编译器的优化问题,
也给每位来读者一个好的回报!

  --相关文章--
· J2EE全面介绍(二) (2007-04-13)
· 项目经验二则:读取war包中的文件及Ant使用中的OutOfMemoryError解决 (2007-04-13)
· 走向J2EE,漫长的道路 (2007-04-13)
· 步入J2EE架构和过程(2) (2007-04-13)
· 步入J2EE架构和过程(1) (2007-04-13)
· 方兴未艾的CORBA (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接