title: Measure elapsed time in Swift
body: <p>How can we measure the time elapsed for running a function in Swift? I am trying to display the elapsed time like this: &quot;Elapsed time is .05 seconds&quot;. Saw that <a href="https://stackoverflow.com/a/1776053/878414">in Java</a>, we can use System.nanoTime(), is there any equivalent methods are available in Swift to accomplish this?</p>
  <p>Please have a look at the sample program:</p>
  <pre class="lang-swift prettyprint-override"><code>func isPrime(_ number: Int) -&gt; Bool {
      var i = 0;
      for i=2; i&lt;number; i++ {
          if number % i == 0, i != 0 {
              return false
          }
      }
      return true
  }
  
  var number = 5915587277
  
  if isPrime(number) {
      print(&quot;Prime number&quot;)
  } else {
      print(&quot;NOT a prime number&quot;)
  }
  </code></pre>