[转载]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
  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 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的对循环的编译优化,你呢??有何见解,请给出你的想法!大家也可以评论一些其它编译器的优化问题,
也给每位来读者一个好的回报!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130811/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-130811/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值