title: Why is Python set intersection faster than Rust HashSet intersection?
body: <p>Here is my Python code:</p>
  
  <pre class="lang-py prettyprint-override"><code>len_sums = 0
  for i in xrange(100000):
      set_1 = set(xrange(1000))
      set_2 = set(xrange(500, 1500))
      intersection_len = len(set_1.intersection(set_2))
      len_sums += intersection_len
  print len_sums
  </code></pre>
  
  <p>Here is my Rust code:</p>
  
  <pre><code>use std::collections::HashSet;
  
  fn main() {
      let mut len_sums = 0;
      for _ in 0..100000 {
          let set_1: HashSet&lt;i32&gt; = (0..1000).collect();
          let set_2: HashSet&lt;i32&gt; = (500..1500).collect();
          let intersection_len = set_1.intersection(&amp;set_2).count();
          len_sums += intersection_len;
      }
      println!("{}", len_sums);
  }
  </code></pre>
  
  <p>I believe these are roughly equivalent. I get the following performance results:</p>
  
  <pre class="lang-none prettyprint-override"><code>time python set_performance.py
  50000000
  
  real    0m11.757s
  user    0m11.736s
  sys 0m0.012s
  </code></pre>
  
  <p>and</p>
  
  <pre class="lang-none prettyprint-override"><code>rustc set_performance.rs -O       
  time ./set_performance 50000000
  
  real    0m17.580s
  user    0m17.533s
  sys 0m0.032s
  </code></pre>
  
  <p>Building with <code>cargo</code> and <code>--release</code> give the same result.</p>
  
  <p>I realize that Python's <code>set</code> is implemented in C, and so is expected to be fast, but I did not expect it to be faster than Rust. Wouldn't it have to do extra type checking that Rust would not?</p>
  
  <p>Perhaps I'm missing something in the way I compile my Rust program, are there any other optimizations flags that I should be using?</p>
  
  <p>Another possibility is that the code is not really equivalent, and Rust is doing unnecessary extra work, am I missing anything?</p>
  
  <p>Python version:</p>
  
  <pre class="lang-none prettyprint-override"><code>In [3]: import sys
  
  In [4]: sys.version
  Out[4]: '2.7.6 (default, Jun 22 2015, 17:58:13) \n[GCC 4.8.2]'
  </code></pre>
  
  <p>Rust version</p>
  
  <pre class="lang-none prettyprint-override"><code>$ rustc --version
  rustc 1.5.0 (3d7cd77e4 2015-12-04)
  </code></pre>
  
  <p>I am using Ubuntu 14.04 and my system architecture is x86_64.</p>