PHP Performance Optimization

PHP Performance OptimizationMohamed AladdinBlockedUnblockFollowFollowingOct 8, 2018Application performance is not just a technical issue.

Performace can make a business successfull or take it down.

So, if you think few milliseconds are not a big deal, think again.

Why does Performance Matter?There are mainly three reasons why performance is essential.

The first is User experience; if your application takes so long to load, then you are risking your users to switch to competitors or leave bad reviews which will affect the business badly.

The second reason is “Conversions.

” Conversions are the number of users who actually buy your product or download your ebook or generally the users who give money for your services.

A faster website means more conversions and more profit.

The third reason is “Scalability.

” The more requests your application can handle per second, the more traffic you can handle.

For example, if your application can handle a single process in 100ms, it means your application can serve 10 requests per second so if you reduce the application processing time to the half, your application capacity will double.

Performance Case Studies:Firefox: when they made the download page faster by 2.

2 seconds, they got more 10 million downloads.

Shopzilla: when their website became 5 seconds faster, the conversion rate has increased by 7–12%.

Bing: when Microsoft search engine site became 1s slower, they lost 2.

8% of their revenue.

Yahoo!: same as Bing, when it is 0.

4s slower they got 5–9% less traffic.

So, whatever the kind of business your application does, a faster application means more revenue.

PHP Performance Optimization:- Choose The Right VersionWell, this one is obvious especially after PHP7 which have the best performance among the old PHP versions.

I will not make a comparison here between PHP versions since there are so many articles discussed that, but you still can take a look at the following chart showing the performance of PHP versions run by different CMSs.

– PHP Micro-OptimizationMicro-Optimization is the minor changes in your code that improve your application performance.

For example, if you are going to use a for-loop, it is always better to calculate the length in advance.

The next image shows the result of for-loop with 1000 keys with 1-byte values are given.

You can notice the improvement in the execution time.

Regardless of the little improvement happening by the Micro-Optimization but it still not enough.

 You can get more Micro-Optimization insight on phpbench.

com.

– XHProf: Profiling PHP CodeAfter we have seen the limited value of PHP Micro-Optimization, it is time to determine precisely which part of our code is slow without the need to guess using a profiling tool called XHProf.

After installing and configuring XHProf on your server, ‘you can find a tutorial on how to install it on youtube.

’ XHProf will append header and footer to all your PHP scripts and generate a report where you can find all the executed functions, the execution time, and the number of calls of each function.

The next image is the profiling result of a products-page built with laravel framework.

It shows the getProductData() has been called 40 times with total execution time of 5 seconds.

Well, at first glance you can expect an “N+1” problem.

Which means if you fix it, your products page will be 5 seconds faster.

The N+1 problem happens when you query a relational database to retrieve data from two different tables.

For example, you query for all products to get Names and Ids.

And then for each product, you query another table to get details such as available colors, or availability in stock.

Usually it can be fixed by rewriting the query to retrieve all your data in one or two queries at maximum.

We will discuss it later in Database query optimization article.

CachingImplementing a caching layer should have an impact taking the load out of your database and decrease the time retrieving frequently required data.

Both Memcache or Redis are widely used for caching; however, Redis has some competitive advantages such as syncing data to disc so when rebooting server data will not be lost, and it has built-in master/slave replication so you can easily scale up redis cluster as your application grows.

MySQL Performance OptimizationThe most important thing when it comes to Database Performace Optimization is to pick the right one.

You should decide…codeburst.

ioWrite clean code and get rid of code smells with real life examplesCode smells are a set of common signs which indicate that your code is not good enough and it needs refactoring to…codeburst.

ioMore to Read: – Write clean code and get rid of code smells with real life examples- Software Architecture: Architect Your Application with AWS- Software Architecture — The Difference Between Architecture and Design- Advanced Coding Skills, Techniques, and Ideas✉️ Subscribe to CodeBurst’s once-weekly Email Blast, ????.Follow CodeBurst on Twitter, view ????️ The 2018 Web Developer Roadmap, and ????️ Learn Full Stack Web Development.

.

. More details

Leave a Reply