I am trying to solve programming challenges on www.interviewstreet.com using PHP. Are there certain techniques I should keep in mind that will optimize the efficiency of the algorithm? For example, I read that using PHP's language constructs versus function calls are faster. Is this true? Any other tips?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Not using functions isn't the mark of algorithmic efficiency. Algorithms themselves are language-agnostic, they are well-defined ways to do something that can be implemented in most any language. Algorithmic efficiency is far more concerned with ideas like "given input of size n, how many steps does a algorithm need to compute answer y?" and "if I increase the input, how does that affect the algorithm?"
Imagine this - what if you were going to sort 100 million items in a list? If your function was something like bubble sort, not using a function call will save you some microsecond of processing, but the actual sorting will take significantly longer and dwarf any gains from not using function calls or any other number of things (plus, not breaking your code up is usually a bad design and bad for maintenance!).
Algorithmic efficiency is usually noted with Big-O Notation and is part of complexity theory. They also relate heavily to data structures, which are things like linked lists, arrays, trees, graphs, etc.
You probably want to read about basic foundational algorithms, data structures and ideas, like:
For books, if you're interested, there are:
PS - many times you'll see people who want to optimize the hell out of their code to squeeze as much performance out of it as possible. What should really be asked first is if they picked the right algorithm to do it and are using the correct data structures.