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 > Why is it so slow?

Why is it so slow?
Thread Tools
Fresh-Faced Recruit
Join Date: Sep 2000
Location: Germany
Status: Offline
Reply With Quote
May 13, 2001, 07:37 AM
 
I have the following lines in my code:
(testString is an NSMutableString)
Code:
while ([testString rangeOfString:@"\n"].length>0) { [testString replaceCharactersInRange:[testString rangeOfString:@"\n"] withString:@"<br>"]; }
This is a VERY slow solution. The one from the TextEdit sample code seems to be much faster, but it's too large for my simple application (IMHO). Any ideas how to speed this up?

------------------
Phure (aka BlackMac.de)
He who knows does not talk - He who talks does not know!

[This message has been edited by Phure (edited 05-13-2001).]
Phure (aka BlackMac.de)
He who knows does not talk - He who talks does not know!
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 13, 2001, 10:15 AM
 
Code:
NSRange curRange; for (curRange = [testString rangeOfString:@"\n"]; curRange.length>0; ) { [testString replaceCharactersInRange:curRange withString:@"<br>"]; }
...maybe? It was typed in here so who knows. But it seems like searching for the range twice could be one hit to avoid. I'd do something like the above instead of calling -rangeOfString twice.

Also, if you look at the docs you'll notice -rangeOfString: calls -rangeOfString :options: which in turn calls -rangeOfString :options:range:. If you really want to optimize it you should avoid the chain calls like that - use -rangeOfString :options:range: instead of -rangeOfString.

Anyone else with anything to add?

[disabled smiles]

[This message has been edited by IamBob (edited 05-13-2001).]
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 13, 2001, 10:49 AM
 
Yes, it's definitely inefficient -- think of all the realloc()ing and memory moving that needs to be done on each -replaceCharacters call if it's a big string.

Typically it's better to build up a new NSMutableString, searching through the first string iteratvely, and adding the necessary substrings to the new NSMutableString. I.e, whenever you find a match, add the substring since the last match then add the replacement string, wash, rinse, repeat. You can use -rangeOfString:options:range: to limit the range of the search to just the areas you haven't searched yet.

However, for the simple substitution you're doing, there's a much easier way:

newString = [[testString componentsSeparatedByString:@"\n"] componentsJoinedByString:@"<br>"];

[If you must modify the testString instance in place, then just add [testString setString:newString] afterwards.]

BTW, you might want to replace with @"<br>\n" so the resulting text is somewhat readable, but that's up to you :-)
     
Phure  (op)
Fresh-Faced Recruit
Join Date: Sep 2000
Location: Germany
Status: Offline
Reply With Quote
May 13, 2001, 11:05 AM
 
"for" doesn't work. I get an endless loop, it does not reinitialize curRange, which is needed, because we get less \n s each time I replace one. Thanks though.
As for lindbergs idea: yes! I think I'm somewhat stupid, it looks so simple, even I could have thought of it :/ .
The result does not need to be readable, it's just meant for interpretation by the computer.

Thank you guys

------------------
Phure (aka BlackMac.de)
He who knows does not talk - He who talks does not know!
Phure (aka BlackMac.de)
He who knows does not talk - He who talks does not know!
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 13, 2001, 01:17 PM
 
Cool. I didn't think the "for" would work but I wasn't sure. I just figured it showed what I was thinking in a compact/semi-understandable way.

Good to know there's an even easier way to do this though - that's cool.
     
   
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 12:34 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