【问题标题】:java looping - declaration of a Class outside / inside the loop [duplicate]java循环 - 在循环外/内声明一个类[重复]
【发布时间】:2011-02-23 11:38:24
【问题描述】:

可能的重复:
Which loop has better performance? Why?
Which is optimal ?
Efficiency of Java code with primitive types

循环时,例如:

for ( int j = 0; j < 1000; j++) {}; 我需要实例化 1000 个对象,我在循环内声明对象与在循环外声明它有什么不同??

 for ( int j = 0; j < 1000; j++) {Object obj; obj =}   

对比

Object obj; 
 for ( int j = 0; j < 1000; j++) {obj =}

很明显,对象只能从循环范围或围绕它的范围访问。但我不明白性能问题、垃圾收集等。

最佳做法是什么?谢谢

【问题讨论】:

    标签: java loops


    【解决方案1】:

    第一种形式更好。限制变量的范围使读者更容易理解它在哪里以及如何使用。

    在性能方面,有限范围也有一些小优势,您可以在 another answer. 中了解这些信息,但这些问题是代码理解的次要问题。

    【讨论】:

      【解决方案2】:

      没有区别。编译器会将它们优化到相同的位置。

      【讨论】:

      • 不正确。连续尝试其中两个并比较字节码。
      • 字节码不重要。它是 JIT 将其优化为重要的机器代码的内容。
      【解决方案3】:

      我已经在我的机器上测试了这个问题,10000 个实例的差异大约是 2-4 毫秒,我测试了所有类型的东西,比如实例化和赋值:

      int i=0;
      

      比较:

      int i;
      i=0;
      

      这是我用来测试的代码,当然是为了测试而改的,在机器达到优化之前有一个初步的平衡效果,你测试一下就清楚了:

      package initializer;
      public final class EfficiencyTests {
      private static class Stoper {
          private long initTime;
          private long executionDuration;
      
          public Stoper() {
          // TODO Auto-generated constructor stub
          }
          private void start() {
              initTime = System.nanoTime();
          }
          private void stop() {
              executionDuration = System.nanoTime() - initTime;
          }
          @Override
          public String toString() {
              return executionDuration + " nanos";
          }
      }
      
      private static Stoper stoper = new Stoper();
      
      public static void main(String[] args) {
          for (int i = 0; i < 100; i++) {
              theCycleOfAForLoop(100000);
              theCycleOfAForLoopWithACallToSize(100000);
              howLongDoesItTakeToSetValueToAVariable(100000);
              howLongDoesItTakeToDefineAVariable(100000);
              System.out.println("\n");
          }
      }
      private static void theCycleOfAForLoop(int loops) {
          stoper.start();
          for (int i = 0; i < loops; i++);
          stoper.stop();
          System.out.println("The average duration of 10 cycles of an empty 'for' loop over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);
      }
      private static void theCycleOfAForLoopWithACallToSize(int loops) {
          ArrayList<Object> objects=new ArrayList<Object>();
          for (int i = 0; i < loops; i++)
              objects.add(new Object());
          stoper.start();
          for (int i = 0; i < objects.size(); i++);
          stoper.stop();
          System.out.println("The average duration of 10 cycles of an empty 'for' loop with call to size over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);
      
      }
      private static void howLongDoesItTakeToSetValueToAVariable(int loops) {
          int value = 0;
          stoper.start();
          for (int i = 0; i < loops; i++) {
              value = 2;
          }
          stoper.stop();
          System.out.println("The average duration of 10 cycles of setting a variable to a constant over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);
      }
      private static void howLongDoesItTakeToDefineAVariable(int loops) {
          stoper.start();
          for (int i = 0; i < loops; i++) {
              int value = 0;
          }
          stoper.stop();
          System.out.println("The average duration of 10 cycles of initializing and setting a variable to a constant over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);
      
      }
      
      private static void runAForLoopOnAnArrayOfObjects() {
      // TODO Auto-generated method stub
      
      }}
      

      如果你减少另一个的时间,你可以得出一个需要多长时间......(如果你明白我的意思)

      希望这可以节省您一些时间。

      您需要了解的是,我测试了这些东西以优化我的平台的绘画更新循环并且它有所帮助。 亚当。

      【讨论】:

        猜你喜欢
        • 2014-06-15
        • 2015-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 1970-01-01
        • 1970-01-01
        • 2015-08-31
        相关资源
        最近更新 更多