title: How to compare characters in Swift efficiently
body: <p>I have a function in Swift that computes the <a href="http:///https://en.wikipedia.org/wiki/Hamming_distance" rel="nofollow noreferrer">hamming distance</a> of two strings and then puts them into a connected graph if the result is 1. </p>
  
  <p><em>For example, <strong>read</strong> to <strong>hear</strong> returns a hamming distance of 2 because <strong><code>read[0] != hear[0]</code></strong> and <strong><code>read[3] != hear[3]</code></strong>.</em></p>
  
  <p>At first, I thought my function was taking a long time because of the quantity of input (8,000+ word dictionary), but I knew that several minutes was too long. So, I rewrote my same algorithm in Java, and the computation took merely 0.3s.</p>
  
  <p>I have tried writing this in Swift two different ways:</p>
  
  <hr>
  
  <h2>Way 1 - Substrings</h2>
  
  <pre><code>extension String {
  
      subscript (i: Int) -&gt; String {
          return self[Range(i ..&lt; i + 1)]
      }
  
  }
  
  private func getHammingDistance(w1: String, w2: String) -&gt; Int {
      if w1.length != w2.length { return -1 }
  
      var counter = 0
      for i in 0 ..&lt; w1.length {
          if w1[i] != w2[i] { counter += 1 }
      }
  
      return counter
  }
  </code></pre>
  
  <p><strong>Results:</strong> 434 seconds</p>
  
  <hr>
  
  <h2>Way 2 - Removing Characters</h2>
  
  <pre><code>private func getHammingDistance(w1: String, w2: String) -&gt; Int {
      if w1.length != w2.length { return -1 }
  
      var counter = 0
      var c1 = w1, c2 = w2      // need to mutate
      let length = w1.length
  
      for i in 0 ..&lt; length {
          if c1.removeFirst() != c2.removeFirst() { counter += 1 }
      }
  
      return counter
  }
  </code></pre>
  
  <p><strong>Results:</strong> 156 seconds</p>
  
  <hr>
  
  <h2>Same Thing in Java</h2>
  
  <p><strong>Results:</strong> 0.3 seconds</p>
  
  <hr>
  
  <h2>Where it's being called</h2>
  
  <pre><code>var graph: Graph
  
  func connectData() {
      let verticies = graph.canvas // canvas is Array&lt;Node&gt;
                                   // Node has key that holds the String
  
      for vertex in 0 ..&lt; verticies.count {
          for compare in vertex + 1 ..&lt; verticies.count {
              if getHammingDistance(w1: verticies[vertex].key!, w2: verticies[compare].key!) == 1 {
                  graph.addEdge(source: verticies[vertex], neighbor: verticies[compare])
              }
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>156 seconds is still far too inefficient for me. What is the absolute most efficient way of comparing characters in Swift? Is there a possible workaround for computing hamming distance that involves not comparing characters?</p>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p><strong>Edit 1:</strong> I am taking an entire dictionary of 4 and 5 letter words and creating a connected graph where the edges indicate a hamming distance of 1. Therefore, I am comparing 8,000+ words to each other to generate edges.</p>
  
  <p><strong>Edit 2:</strong> Added method call.</p>