title: How to resolve the Python List looping performance below
body: <p>I have a Python code below and it hits out of memory error for 100Mil loop append. Java with same code doesn't have this issue at all without any tuning.</p>
  <ol>
  <li><p>Anyway to tune using the python command or like Java Hotspot JVM Command?</p>
  </li>
  <li><p>Anyway to tune using coding way to make it run faster and utilize lesser memory.</p>
  </li>
  </ol>
  <br/>
  <pre><code>import datetime;
  
  mylist = []
  
  before = datetime.datetime.now()
  
  for _ in range(100000000):
      mylist.append(datetime.datetime.now())
  
  print(&quot;List length--&gt;&quot; , len(mylist))     
     
  after = datetime.datetime.now()
  
  print ('Python time taken in seconds---&gt;', (after - before).seconds)
  </code></pre>
  <p>Post notes:</p>
  <p>Memory leak detection on this &quot;datetime.datetime.now()&quot;</p>
  <p>Sharing my java code here. It works more than 10 times faster without JVM tuning yet and process completed in about 6 seconds .</p>
  <p>Anyway, Java does a much better garbage collection job than Python. Normally Java won't crash in this kinda simple operation since 20 years back.
  <a href="https://www.snaplogic.com/glossary/python-vs-java-performance" rel="nofollow noreferrer">https://www.snaplogic.com/glossary/python-vs-java-performance</a></p>
  <p>Note: Change from System.currentTimeMillis() to New Date() doesn't make different.</p>
  <pre><code>package demo;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Performance {
  
      public static void main(String[] args) {
          List&lt;Long&gt; mylist = new ArrayList&lt;Long&gt;();
  
          long before = System.currentTimeMillis();
  
          for (int i = 0; i &lt; 100000000; i++) {
              mylist.add(System.currentTimeMillis());
  
          }
  
          long after = System.currentTimeMillis();
          System.out.println(&quot;Java time taken in miliseconds---&gt;&quot; + (after - before) );
  
      }
  
  }
  </code></pre>