Measuring function execution time

This is a very quick one. For an app I’m working on I wanted to measure how long it took a given function to complete. 

I typically don’t do this, as most functions are very fast, but this one involved a server fetch and some background processing. Anyhow, I’m getting distracted, here it is -easy as pie-:

 

let startTime = Date()

 

//Code

 

let endTime = Date()

let timeElapsed: Double = endTime.timeIntervalSince(startTime)

 

print(“Time taken \(timeElapsed) seconds”)

 

Questions / comments / suggestions? I’m at @MarcMasVi 

Marc

 

 

Get Date Day, Month or Year in Swift 3.0

Just a quick update to show how to get Date Day, Month or Year in Swift 3

//Here I’m creating the calendar instance that we will operate with:

let calendar = NSCalendar.init(calendarIdentifier: NSCalendar.Identifier.gregorian)

     

 

//Now asking the calendar what month are we in today’s date:

let currentMonthInt = (calendar?.component(NSCalendar.Unit.month, from: Date()))!

       

 

//Now asking the calendar what year are we in today’s date:

let currentYearInt = (calendar?.component(NSCalendar.Unit.year, from: Date()))!

 

Hope this proves useful.

If you would have any suggestions on how to improve reach me @MarcMasVi in Twitter. 

 Marc