title: Compare arrays in swift
body: <p>Trying to understand how swift compares arrays.</p>
  
  <pre><code>var myArray1 : [String] = ["1","2","3","4","5"]
  var myArray2 : [String] = ["1","2","3","4","5"]
  
  // 1) Comparing 2 simple arrays
  
  if(myArray1 == myArray2) {
      println("Equality")
  } else {
      println("Equality no")
  }
  // -&gt; prints equality -&gt; thanks god
  
  // 2) comparing to a "copy" of an array
  
  // swift copies arrays when passed as parameters (as per doc)
  func arrayTest(anArray: [String]) -&gt; Bool {
      return anArray == myArray1
  }
  
  println("Array test 1 is \(arrayTest(myArray1))")
  println("Array test 2 is \(arrayTest(myArray2))")
  // equality works for both
  
  myArray2.append("test")
  println("Array test 2 is \(arrayTest(myArray2))")
  // false (obviously)
  
  myArray2.removeAtIndex(5)
  println("Array test 2 is \(arrayTest(myArray2))")
  // true
  </code></pre>
  
  <p>Apple says there are optimisations behind the scene on array copies. Looks like sometimes - not always - structures are actually copied or not.</p>
  
  <p>That said,</p>
  
  <p>1) is == iterating over all the array to perform a element-based comparison ? (looks like it)
  -> How about performance / memory usage on very large arrays then ?</p>
  
  <p>2) Are we sure == will ever return true if all elements are equal ? I have bad memories of == on Java Strings</p>
  
  <p>3) Is there a way to check if myArray1 and myArray2 are technically using the same "memory location" / pointer / etc. ? i'm after understanding how the optimisation works and potential caveats.</p>
  
  <p>Thanks.</p>