Re: Fastest way to build an attributed string
Re: Fastest way to build an attributed string
- Subject: Re: Fastest way to build an attributed string
- From: glenn andreas <email@hidden>
- Date: Fri, 14 Jan 2005 08:59:42 -0600
On Jan 14, 2005, at 5:26 AM, Fabian Lidman wrote:
Hello,
Q: What is the fastest way to create an attributed string from a
sequence of string/dictionary pairs?
The application i'm working on needs to build an attributed string
(specifically, an NSTextStorage) from data in an XML file. Depending
on the size of the XML file, various number of dictionary/string pairs
are concatenated into an attributed string:
while(thereIsMoreData) {
attributeRun = [xmlFile nextAttributeRun];
string = [xmlFile nextString];
[[textStorage mutableString] appendString: string];
[textStorage setAttributes: attributeRun range:
NSMakeRange([textStorage length] - [string length], [string length])];
}
For a short XML file, this takes close to no time. For a longer file,
the execution time seems to grow almost exponentially. A closer look
in Shark reveals that the -setAttributes:range: call stands for about
80% of time. Peeking inside -setAttributes:range shows that it is
doing a lot of dictionary comparison, possibly in an attempt to save
memory by ensuring there are no duplicate attribute dictionaries.
Memory, however, is less important than speed in this particular case.
For a really large XML file it may take several minutes to create the
attributed string.
Is there a faster way to create an attributed string?
As a quick "pre-coffee" guess, I'd try something like
while (thereIsMoreData) {
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString: [xmlFile nextString] attributes: [xlmFile
nextAttributeRun]];
[textStorage appendAttributeString: attrString];
[attrString release];
}
Though, of course, running through Shark would still be a good idea
(since appendAttributeString may have some of the same properties that
setAttributes did)
Glenn Andreas email@hidden
<http://www.gandreas.com/> oh my!
Mad, Bad, and Dangerous to Know
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden