Efficient String Manipulation With Objective-C
Regex techniques with PHP, Javascript, Python and Ruby are the most popular (I did it using PHP), but I was curious as what an implementation would look like using Objective-C. It was my theory that using regular expressions would incur more overhead and than using a “string split” technique. Apple does a fantastic job with making their framework api’s pretty darn efficient, but I had a feeling that with this type of simple manipulation using a regular expression would be like lighting a grill with an atom bomb. The results weren’t so dramatic, but none the less it does turn out that using componentsSeparatedByString is a better use of resources....for a single manipulation. When iterating and manipulating multiple strings I prefer to use regex.
When I iterated over the string 1000x the time to execute was almost the exact same.
I even tested two different regular expressions to make sure I wasn’t being to liberal with my pattern. The difference was negligible.
Test String: "hEll0 WoRLD please hElP me"
Standard Regular Expression: "(\\w)+\\s?"
Boundary Regular Expression: "(\\b\\w)"
Single (regular regex)
Time to parse using regex: 0.003947
Time to parse using componentsSeparatedByString: 0.001246
Single (boundary regex)
Time to parse using regex: 0.003747
Time to parse using componentsSeparatedByString: 0.000668
1000x (Regular)
Time to parse using regex: 0.511170
Time to parse using componentsSeparatedByString: 0.538784
1000x (Boundary)
Time to parse using regex: 0.555807
Time to parse using componentsSeparatedByString: 0.485708
Here is the test project


