title: Why is this hashmap search slower than expected?
body: <p>What is the best way to check a hash map for a key?
  Currently I am using this:</p>
  
  <pre class="lang-rust prettyprint-override"><code>let hashmap = HashMap::&lt;&amp;str, &amp;str&gt;::new();  // Empty hashmap
  
  let name = "random";
  
  for i in 0..5000000 {
      if !hashmap.contains_key(&amp;name) { 
          // Do nothing
          }
  }
  </code></pre>
  
  <p>This seems to be fast in most cases and takes 0.06 seconds when run as shown, but when I use it in this following loop it becomes very slow and takes almost 1 min on my machine. (This is compiling with <code>cargo run --release</code>).
  The code aims to open an external program, and loop over the output from that program.</p>
  
  <pre class="lang-rust prettyprint-override"><code>let a = vec!["view", "-h"]; // Arguments to open process with
  let mut child = Command::new("samtools").args(&amp;a)
                                          .stdout(Stdio::piped())
                                          .spawn()
                                          .unwrap();
  
  let collect_pairs = HashMap::&lt;&amp;str, &amp;str&gt;::new();
  
  if let Some(ref mut stdout) = child.stdout {
      for line in BufReader::new(stdout).lines() {
          // Do stuff here          
          let name = "random";
          if !collect_pairs.contains_key(&amp;name) {
              // Do nothing
          }
      }
  }
  </code></pre>
  
  <p>For some reason adding the <code>if !collect_pairs.contains_key(</code> line increases the run time by almost a minute. The output from child is around 5 million lines. All this code exists in <code>fn main()</code></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This appears to fix the problem, resulting in a fast run time, but I do not know why the <code>!hashmap.contains_key</code> does not work well here:</p>
  
  <pre class="lang-rust prettyprint-override"><code>let n: Option&lt;&amp;&amp;str&gt; = collect_pairs.get(name);
  if match n {Some(v) =&gt; 1, None =&gt; 0} == 1 {
    // Do something
  }
  </code></pre>