Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > More optimization questions

More optimization questions
Thread Tools
Forum Regular
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jun 22, 2002, 02:12 AM
 
Hi again,

Here are some other questions about optimization :

Suppose I have a string with data I want to remove inside it.
I have two solutions :
1) using an NSScanner and copy the data I want into another string.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">NSMutableString *resultString = [[NSMutableString alloc] init];
NSScanner *originalStringScanner = [[NSScanner alloc] initWithString:originalString];
if ( [originalStringScanner scanUpToString:@&quot;new Array&quot; intoString:NULL] &amp;&amp;
[originalStringScanner scanUpToString:@&quot;(&quot; intoString:NULL] )
{
while ( [originalStringScanner scanUpToString:@&quot;new&quot; intoString:&amp;tempString] )
{
if ( [originalStringScanner isAtEnd] == NO )
{
[resultString appendString:tempString];
if ( [originalStringScanner scanUpToString:@&quot;(&quot; intoString:NULL] )
scanLocation = [originalStringScanner scanLocation];
}
}
[originalStringScanner setScanLocation:scanLocation];
if ( [originalStringScanner scanUpToString:@&quot;&quot; intoString:&amp;tempString] )
[resultString appendString:tempString];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">2) using an NSMutableString with deleteCharactersInRange method
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">// here originalString is a NSMutableString
NSScanner *originalStringScanner = [[NSScanner alloc] initWithString:originalString];
char length = (char)[@&quot;new Array&quot; length]; //this is to understand what length is…
NSRange myRange;
while ( [originalStringScanner isAtEnd] == NO )
{
if ([originalStringScanner scanUpToString:@&quot;new Array&quot; intoString:NULL])
{
myRange = NSMakeRange(originalStringScanner scanLocation, length);
[originalString deleteCharactersInRange:myRange];
}
}
// this second search is necessary, but implemented differently, however I am quite sure it is longer to execute since it scan the string from beginning each time…
do
{
myRange = [originalString rangeOfString:@&quot;new Dictionary&quot;];
[originalString deleteCharactersInRange:myRange];
} while (myRange.location != NSNotFound);</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I suppose the first is faster since it parses the string only once, however I do not know how these methods are implemented, and maybe someone here know some goos details on them.

Thanks
Imagine that my signature is here...
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jun 22, 2002, 02:33 AM
 
Benchmark it?
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:28 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2