Efficient String Manipulation With Objective-C

One of the best interview questions that I have ever been asked, and now ask myself to potential candidates, is create a function that will transform any set of words into an acronym.  The reason why I like this question is that it shows how well the candidate understands basic string manipulation, problem solving and how efficient their technique is for the string manipulation.  Regardless of the language that is chosen the two techniques use by most are to either use some sort of regular expression or split.

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

Zend Framework Dynamic Site - In Production

Back in April I wrote a blog post discussing my concept of having a dynamic site using the Zend Framework.  In addition, I posted an some example code of how everything works.  I am a firm believer that one should practice what they preach and two different sites are now in production using the framework that I wrote and so far it works beautifully.  I had to make a few changes to the route to allow for module exceptions.

Towards the end of the project there was a request to have a search functionality and also custom forms.  Normally this wouldn't be an issue what so ever, but the way that the custom route is setup all requests are send to the default module/index controller/index action.  I modified the route to ignore any request that started with search or forms and route those requests to the search or forms module.  The regex is easily modified to allow for other exceptions.

Custom Regex:

I also setup the ability to add in meta keywords and meta description tags in the content.xml file.  Finally, both sites use the EXACT same doc root and dynamic site framework.  Since both sites use the same layouts, just different menus and different background images, I didn't want to duplicate a lot of code.  So in the setup page display plugin I am able to transverse the content mapping file based upon not only the request, but the url host name to display the proper layout.