Java编译器真正的编译优化了循环吗? — JAVA频道

 

数据挖掘工具

 

数据挖掘研究院

 

 

  数据挖掘交友

  数据挖掘研究院

  数据挖掘交友

  数据挖掘论坛

数据挖掘论坛

此文章只是用来在java编译器进行循环化方面的验证,本文是有感而发,并希望求得大家的分析结果,这里引用了一位网友的话(没有人身攻击的色彩):
///////////////////////////
对于Vector v=new Vector()若v中包含了10000个对象,我们进行一个loop如下
for(int I=0; I大家一定会用如下的写法吧 
int size = v.size(); for(int I=0; I///////////////////////////
但有些网友说(只是为了说明问题而已,无攻击色彩):
不能用完全解释语言过程来理解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      //do something
  6.     }
  7.   }
  8. }
数据挖掘论坛
结果还是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 index. Each component in this vector with 
  10.      * an index greater or equal to the specified index is 
  11.      * shifted upward to have an index one greater than the value it had 
  12.      * previously. 

  13.      *
  14.      * The index must be a value greater than or equal to 0 
  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.)

  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.    }
数据挖掘论坛
这象上面的similarVector对象一样,v.size()会每次调用Vector中的size()方法,10000次的调用一次也不会少吧!!
所以就我自身来说我不会相信java的对循环的编译优化,你呢??有何见解,请给出你的想法!大家也可以
[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:Java中基本数据类型与流的操作方法 — JAVA频道
下一篇:Java编程学习:自己DIY一个JSP日历 — JAVA频道
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • 尚学堂J2EE和MLDN的J2EE视频教程哦
  • 有谁知道ERROR:JDWP Unable to get
  • com.microsoft.sqlserver.jdbc.SQLServerEx
  • 急~Eclipse3.3语言包,VEP插件
  • 毕业5年了,大家一个月全部收入能拿多少(税
  • org.hibernate.exception.ConstraintViolat
  • hibernate抛出could not fetch initial val
  • hibernate left outer join 出错 Path expe
  • myeclipse5.1.0和myeclipse6.0有什么区别。
  • spring如何动态获取bean,如何动态调用getB
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • 不安装jre能运行JAVA程序吗?
  • Java的模板引擎Velocity用户手册
  • 成为java高手的八大条件
  • 简化Java代码的技巧
  • JAVA的网络功能与编程
  • 用java读取各种计算机文件系统的文件列表
  • 怎样拿下SUN公司的SCJP认证?
  • Borland以全额现金交易收购VMGEAR
  • 用JNI技术提高Java的性能
  • 用Java编写HTML文件分析程序
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静