ios - Performance issues while parsing dates -


i having performance issues in ios app while transforming dates come in 2013-12-19 00:00:00.000000 format medium style date (dec 25, 2014) , double value (epoch). according xcode profiler 2 functions process (bellow) taking approximately 60% of execution time

i know how improve code or if there more efficient way need.

static func getmediumdate(datestring: string) -> (nsstring)? {      // the: yyyy-mm-dd     let shordate = datestring[datestring.startindex..<datestring.startindex.advancedby(10)]      let dateformatter = nsdateformatter()     dateformatter.locale = nslocale(localeidentifier: "en_us")     dateformatter.dateformat = "yyyy-mm-dd"      let stringformatter = nsdateformatter()     stringformatter.locale = nslocale(localeidentifier: "en_us")     stringformatter.dateformat = "yyyy-mm-dd"     stringformatter.datestyle = .mediumstyle      let newdate = dateformatter.datefromstring(shordate)      if (newdate != nil){         return stringformatter.stringfromdate(newdate!)     }else{         return nil     } }  static func getsortdate(datestring:string) -> double{      // the: yyyy-mm-dd     let shordate = datestring[datestring.startindex..<datestring.startindex.advancedby(10)]      let dateformatter = nsdateformatter()     dateformatter.locale = nslocale(localeidentifier: "en_us")     dateformatter.dateformat = "yyyy-mm-dd"      let newdate = dateformatter.datefromstring(shordate)      let value = newdate?.timeintervalsince1970      if value < dbl_min{         return 0     }else if value > dbl_max{         return dbl_max     }else if value != nil{         return value!     }else{         return 0     } } 

nsdateformatter notoriously slow. should create once, cache , reuse same instance rather creating new instance every time.

for example, can following:

extension nsdateformatter {     private static let standarddateformatter: nsdateformatter = {         let dateformatter = nsdateformatter()         dateformatter.locale = nslocale(localeidentifier: "en_us")         dateformatter.dateformat = "yyyy-mm-dd"         return dateformatter     }() } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -