Re: Swift: How to determine if a Character represents whitespace?
Re: Swift: How to determine if a Character represents whitespace?
- Subject: Re: Swift: How to determine if a Character represents whitespace?
- From: Quincey Morris <email@hidden>
- Date: Fri, 03 Apr 2015 21:14:50 +0000
On Apr 3, 2015, at 13:18 , Charles Jenkins <email@hidden> wrote:
>
> is there a way in the playground for use to test addresses to make sure attrStr.string as NSString doesn’t perform a copy?
I doubt it. This is the best I could come up with in a couple of minutes:
> import Cocoa
>
> let notWhitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet
> let count = 500000
>
> let aString: String = String (count: count, repeatedValue: Character ("A"))
> let aNSString: NSString = ("" as NSString).stringByPaddingToLength (count, withString: "A", startingAtIndex: 0)
>
> let date1 = NSDate ()
> let bString: String = aNSString as String
> let date2 = NSDate ()
> let time2 = date2.timeIntervalSinceDate(date1)
>
> let date3 = NSDate ()
> let bNSString: NSString = aString as NSString
> let date4 = NSDate ()
> let time4 = date4.timeIntervalSinceDate(date3)
>
> let attrStr = NSAttributedString (string: aNSString)
>
> let date5 = NSDate ()
> let range5 = attrStr.string.rangeOfCharacterFromSet(notWhitespace, options: NSStringCompareOptions.allZeros)
> let date6 = NSDate ()
> let time6 = date6.timeIntervalSinceDate(date5)
>
> let date7 = NSDate ()
> let range7 = (attrStr.string as NSString).rangeOfCharacterFromSet(notWhitespace, options: NSStringCompareOptions.allZeros)
> let date8 = NSDate ()
> let time8 = date8.timeIntervalSinceDate(date7)
Playground results:
time2: 0.3328
time4: 0.1817
time6: 0.0022
time8: 0.0017
Since the “rangeOfCharacter” scans terminate at the first character, this suggests that there’s no real conversion in the last case, which is the one you’re interested in. (Also, time6 and time8 don’t vary with the value of ‘count’.) However, generalizing from this seems treacherous. And I may have just Done It Wrong™.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden