Join Justin Yost for an in-depth discussion in this video Macro vs. Micro optimization, part of PHP: Performance Optimization.
- [Instructor] There's a bit of an optimization quandary between both optimizing at the macro verus the micro scale. By this, I mean optimization on the large scale, i.e., macro, and then on the low level problems you might have, i.e., micro optimization. You may have heard the quote before that premature optimization is the root of all evil. The point of this quote from Donald Knuth is that we should, generally speaking, write code without worrying specifically about performance.
If you are writing code that is well engineered and architectured and well tested, most performance problems may not be a real issue in most use cases. Your real performance gains are when you solve the major bottlenecks in your code, not the little ones. A classic example of focusing too much on micro optimization over major optimization is wondering which type of function call is the best, a static call, an object call, or a plain function call. In this lesson's exercise files, open up the snippets.txt file.
You want to copy this entire file. Open up your text editor and create a new file, and this file we're going to save as test.php. Now what this file does is if we look all the way at the top, we call a similar function three different ways. All these functions are doing is taking one number, multiplying them together, and then sending it back. The first way, on line four, is just calling it via a standard object call. On line nine, we'll call this function statically, and finally, and on line 15, we'll call this function without instantiating an object.
These functions are doing all very simple, very basic things. The point here isn't necessarily to worry too much about what's going on inside of the function. It's to demonstrate the principle at the end of the day, it doesn't matter how you call the function. It just matters that what you're doing in your code is optimal. If we test and run this function by going to our terminal application and running the command php test.php, you can see that there are tiny little insignificant differences between calling a function statically, as an object, or just as a pure function.
At the end of the day here, we could squeeze a little bit more speed by removing our objects and just calling them as a function, but that's not really the main point. The more important bit here is is to eliminate our loops. Looping over a million things is slow. Don't get so caught up in the micro optimizations you forget the macro optimizations that will actually improve the performance of your application. Using the various tools we've discussed so far to both accurately measure and debug the bottlenecks will give you a good handle on the most bang for your buck when doing performance improvement.
- General optimization techniques and tools
- PHP and Xdebug
- Opcode cache
- Optimization in PHP
- Upgrading PHP
- Macro vs. Micro optimization
- HTTP caching
- HTTP compression