A 31 year old “dog” can definitely learn new tricks. I just finished learning basic algebra in under a week when all of my life I suffered through math. I’ve learned to no longer hate math, and in fact, I’ve found that when I related it to programming, my math ability rose. The moral of the story is that when something doesn’t make sense, try to relate it something you know to make sense.
Optimize Your Code
In math, if you’re off by just one step, your entire calculation could be off, and you’ll never know it. In programming if you’re wrong, the compiler just yells at you. Math is exceptionally brutal, and it didn’t make sense to me. However, once I related math to programming, I learned how to optimize my code to add a series of numbers together without loops. That’s just one of many optimizations that a strong foundation in math can give a programmer. If you hate math like I did, yet you crave clean, maintainable, and optimized code, then I suggest learning the basics of Algebra for free through Khan Academy. It’s not as difficult as you may think.
Aside from pounding out math equations, I’ve learned to use every PHP data structure. The SPL library offers many popular data structures, yet most PHP programmers don’t even use them. My guess is that most people don’t even know that they exist, just like I didn’t for a long time. The data structures offer some speed ups to optimize your code, and in websites with lots of visitors, you can’t take chances. The code simply must work blazingly fast or it will bottleneck your site. Look at WordPress for example. WordPress is excruciatingly slow, and plugins only make it slower.
Don’t get me wrong, WordPress in a great program, and as you can see, I use it. However, it’s not meant to be used as anything more than a blog. I’ve seen what happens when a site gets high traffic when it uses WordPress, and it’s not pleasant. Look under the hood and you’ll see why. The first thing that pops out at me is the fact that the WordPress team builds for PHP 5.2.4 (Majorly outdated) and they use functional programming instead of OOP.
In OOP, you keep your codebase clean by organizing everything into objects. Code often runs much faster when it’s in a class. Take “define” versus “public const MYCONST = 1” for example. When you declare a constant in an object, it’s nice and fast. When you define it in the global namespace, it’s slower. I won’t go into why, and this post into meant to point out the flaws in WordPress, though I advise you to look into the matter on your own if you’re working to optimize your code.
That brings me to more optimizations. Webservers play a critical role in serving your website. In fact, a webserver is THE most critical software in serving your website. You can’t serve a website without one, and therefore, every page you visit goes through a webserver. Apache is the defacto standard for most hosting. It’s been around longer, and it has more features. However, not only is the machine that does less, more secure, it’s also much faster.
Nginx does less than Apache, and it’s designed for speed. Not only that, Google created SPDY to optimize the speed of TCP. (TCP is what HTTP runs on, and each time you make a request for an HTML page, graphic, JS file, or anything else, you make that request through TCP.) Each TCP request makes 9 trips back and forth during its life cycle, between the client (browser) and the server hosting the website. That means that if your browser requests the web page, a style sheet, a JS file, and 7 graphics, you’re requesting 10 total items from the server that require 10*9=90 trips back and forth from your browser to the server. SPDY reduces the number of TCP trips. Plus, Google is trying to replace TCP with QUIC, a “new” protocol based on UDP. UDP isn’t reliable, though like I say in my book, “The New Frontier in Web API Programming,” it’s up to the programmer to make it reliable. QUIC will optimize the speed of the modern internet connectivity instead of relying on a protocol that was optimized for the dawn of the internet in 1974.
Alright, so now your program in reliable, and your webserver is reliable. What about PHP? Standard PHP is notoriously slow, and it’s given PHP a bad name. PHP also has a bad name due to it’s “loosely typed” scripting, and the terrible way that it handles arrays. (In PHP arrays are not true arrays. They’re a combination of hash tables and doubly linked lists. They add unnecessary overhead.) Facebook created HHVM to exponentially speed up PHP, and Hack to give PHP the missing strongly typed form it so desperately needs. I’ve learned most of HHVM, and I’ve found that it’s not much different from how I use standard PHP. The big thing is that it doesn’t use some of the php.ini directives, and it’s configured to be more secure right out of the box. As for Hack, I’m still learning the rest of it, though for the most part it seems pretty straight forward.
If you want to optimize your code for speed, consider the following software.
- Nginx compiled with SPDY
- HHVM (It comes with Hack – Use it to properly optimize your code.)
- SPL Data Structures and the interfaces for the Agile Observer pattern
- Math (Just learn basic algebra. If you learn anything beyond that, you might find it useful, though I don’t see it as a requirement.)
Hopefully you’ve learned something from this article. I wish you well on your project.
Photo Credit: Pixabay