• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Fastest way to replace characters in string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fastest way to replace characters in string


  • Subject: Re: Fastest way to replace characters in string
  • From: Jens Alfke <email@hidden>
  • Date: Thu, 22 Aug 2013 08:06:51 -0700

On Aug 22, 2013, at 3:05 AM, Diederik Meijer | Ten Horses <email@hidden> wrote:

> What actually speeds things up significantly is to revert back to NSString and use NSRegularExpression.
> I now needs seven seconds on iPad3 to handle >600 replacement actions. This is, in itself a VERY long time

Indeed.; that’s incredibly slow. You should be able to do this in a handful of milliseconds. Don’t make your users suffer through a progress meter because you couldn’t find the right optimization :)

The right way to do this is by scanning through the original string and writing to a new mutable string. Using replaceCharacters on a mutable string isn’t much of a speedup because it keeps copying characters in the mutable string over and over (and copying the entire string if its buffer needs to grow.)

create the empty output mutable string with sufficient capacity (i.e. maybe 2x the input string length)
set pos to 0
repeat
	find next instance of marker in original string starting from index ‘pos’
	if none found
		break
	append input characters from ‘pos’ to start of marker to output string
	append replacement characters to output string
	advance ‘pos’ to end of marker
end

I would guess that most algorithms textbooks describe things like this in more detail.

—Jens
_______________________________________________

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


  • Follow-Ups:
    • Re: Fastest way to replace characters in string
      • From: Keary Suska <email@hidden>
References: 
 >Fastest way to replace characters in string (From: Diederik Meijer | Ten Horses <email@hidden>)
 >Re: Fastest way to replace characters in string (From: Wim Lewis <email@hidden>)
 >Re: Fastest way to replace characters in string (From: Diederik Meijer | Ten Horses <email@hidden>)

  • Prev by Date: Re: handleGetURLEvent getting called after applicationDidFinishLaunching
  • Next by Date: Re: handleGetURLEvent getting called after applicationDidFinishLaunching
  • Previous by thread: Re: Fastest way to replace characters in string
  • Next by thread: Re: Fastest way to replace characters in string
  • Index(es):
    • Date
    • Thread