DO NOT DEAL WITH THIS MEMBER
Dec 17, 2013 12:25 PM
(This post was last modified: Dec 17, 2013 01:17 PM by VladHQ.)
How to increase performance of PHP!
Introduction
PHP is a language that compiles the scripts each time a user visits your website. Sometimes, it is a waste to recompile the same exact script. In this tutorial, I will introduce you to caching and other big performance tips.
What is Caching?
Code Caching - store away in hiding or for future use.
Google just defined it for us. Caching is good for any application, especially blogs.
Example
Say we have a script called websites.php which consisted of:
PHP Code
<?php echo 'Google:<br/>'; echo file_get_contents('http://www.google.com/'); echo '<br/>';
echo 'Yahoo:<br/>'; echo file_get_contents('http://www.yahoo.com/'); echo '<br/>';
echo 'Facebook:<br/>'; echo file_get_contents('http://www.facebook.com/'); echo '<br/>'; ?>
It will take about 2-5 seconds depending on the location of the server to make requests to the three websites.
What if the user refreshes the page? Your server would make another and another and another request. That could be just a waste of bandwidth!
But what if we just made a request once and just put it in a file? And upon request we just read from a file? Would that save up resources? YES!
How Do I Cache?
It depends on what you are making. If you are making a search engine, like me, you shouldn't cache because you can just cache your MySQL queries with SQL_CACHE.
For the example's sake, I will use the website example.
PHP Code
<?php // http://www.theukwebdesigncompany.com/articles/php-caching.php
$cachefile = './cache/' . $_SERVER['REMOTE_ADDR'] . '.html'; $minutes = '10'; $cachetime = $minutes * 60;
if(file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) { // Check if the file exists include($cachefile); // if the file is cached, include it die(); } ob_start(); // Make sure to include this
/* Your script goes here */ echo 'Google:<br/>'; echo file_get_contents('http://www.google.com/'); echo '<br/>';
echo 'Yahoo:<br/>'; echo file_get_contents('http://www.yahoo.com/'); echo '<br/>';
echo 'Facebook:<br/>'; echo file_get_contents('http://www.facebook.com/'); echo '<br/>';
file_put_contents($cachefile, ob_get_contents()); // Write to cache ob_end_flush(); ?>
What is Minification?
Minification is the act of making an HTML/CSS/JS file smaller without having to remove the main components. By removing unused spaces you could save 20% of the space right there!
Example
PHP Code
<?php <!DOCTYPE html> <html> <head> <title>My Title</title> <style> body { background-color:yellow; } </style> </head> <body> <h1>HI!</h1> </body> </html>
becomes this:
PHP Code
<?php <!DOCTYPE html><html><head><title>My Title</title><style>body{background-color:yellow;}</style></head><body><h1>HI!</h1></body></html>
How Do I Minify Files?
If you would like to do it on run-time:
• Download this package - https://code.google.com/p/minify/downloa...-2.1.7.zip
• Put this on top of your PHP file:
PHP Code
<?php function minify($content) { require './minify/min/lib/Minify/HTML.php'; require './minify/min/lib/Minify/CSS.php'; require './minify/min/lib/JSMin.php'; $content = Minify_HTML::minify($content, array(
'cssMinifier' => array('Minify_CSS', 'minify'), 'jsMinifier' => array('JSMin', 'minify') )); return $content; }
ob_start('minify');
/* .. */
Compiling JavaScript
This is a small section that in which I will explain how to make your JavaScript about 80% slower.
It is called the Google Closure Compiler. It is an external link that can be found below:
http://closure-compiler.appspot.com/home
Basic Tips- Use commas to separate variables in echo statements.
- Use echo instead of print.
- Avoid loops!
- Use single quotes ('')
- Don't use require_once() or include_once()
- Don't copy variables (see example)
PHP Code
<?php // Bad Way: $string = '<sTrInG>'; $string = htmlentities($string); $string = strtolower($string);
// Good Way $string = htmlentities(strtolower('<sTrInG>')); ?>
- Use PDO or mysqli_* instead of mysql_*
I hope that you guys found this helpful. I will be adding some other stuff a little later, and if anyone knows any tips that should be on here, just reply to the thread. Thanks!
DO NOT DEAL WITH THIS MEMBER
Thanks for the tutorial!
but..
(Dec 17, 2013 12:25 PM)VladHQ Wrote: PHP Code
<?php // http://www.theukwebdesigncompany.com/articles/php-caching.php
$cachefile = './cache/' . $_SERVER['REMOTE_ADDR'] . '.html'; $minutes = '10'; $cachetime = $minutes * 60;
if(file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) { // Check if the file exists include($cachefile); // if the file is cached, include it die(); } ob_start(); // Make sure to include this
/* Your script goes here */ echo 'Google:<br/>'; echo file_get_contents('http://www.google.com/'); echo '<br/>';
echo 'Yahoo:<br/>'; echo file_get_contents('http://www.yahoo.com/'); echo '<br/>';
echo 'Facebook:<br/>'; echo file_get_contents('http://www.facebook.com/'); echo '<br/>';
ob_start(); file_put_contents($cachefile, ob_get_contents()); // Write to cache ob_end_flush(); ?>
[/spoiler] why does it do ob_start(); two times? Is it required or wans't you meant to do it?
-DJS
This is a great tutorial! I don't entirely agree with Minification as it makes it hard for other people to read it. But for those who'd like to it's also great!
I've made this a book mark as a reference just in case I need it 😁
BinKill-Ethical 🤝
Caching: Use APC
"Minification": Use GZip. GZip > minification
Compiling JS: This is not actually compilation of JS it seems, rather "minification" as you so eloquently put it. GZip would cover this if you install it correctly.
DO NOT DEAL WITH THIS MEMBER
Dec 17, 2013 01:18 PM
(This post was last modified: Dec 17, 2013 01:20 PM by VladHQ.)
(Dec 17, 2013 12:30 PM)DJSanderrr Wrote: Thanks for the tutorial!
but..
why does it do ob_start(); two times? Is it required or wans't you meant to do it?
-DJS
I did it on accident. I just fixed it. Thank you.
(Dec 17, 2013 12:33 PM)badbrains Wrote: Caching: Use APC
"Minification": Use GZip. GZip > minification
Compiling JS: This is not actually compilation of JS it seems, rather "minification" as you so eloquently put it. GZip would cover this if you install it correctly.
You can't install APC on shared hosting. Most people use shared hosting right now. You could use both to save space (that's what I do).
(Dec 17, 2013 12:31 PM)t0rch Wrote: This is a great tutorial! I don't entirely agree with Minification as it makes it hard for other people to read it. But for those who'd like to it's also great!
I've made this a book mark as a reference just in case I need it 😁
BinKill-Ethical 🤝
Thanks for the awesome feedback. If you want people to view your code, just provide a link to the actual unminified full page.
I will be adding a Cloudflare setup guide, as it does make your page load faster.
(Dec 17, 2013 01:18 PM)VladHQ Wrote: I did it on accident. I just fixed it. Thank you.
You can't install APC on shared hosting. Most people use shared hosting right now. You could use both to save space (that's what I do).
If you're doing any serious project (that requires caching and compression) on shared hosting, you have a problem. Just my two cents.
DO NOT DEAL WITH THIS MEMBER
(Dec 17, 2013 01:20 PM)badbrains Wrote: If you're doing any serious project (that requires caching and compression) on shared hosting, you have a problem. Just my two cents.
Well I am creating a search engine right now. I have developed it for a little less than a year, and I find it better to use shared hosting as it is maintained, and I use my laptop to host the crawler in Python. It's not that bad.
DO NOT DEAL WITH THIS MEMBER
I would live to hear some more feedback on this tutorial!
(Dec 20, 2013 08:22 PM)VladHQ Wrote: I would live to hear some more feedback on this tutorial!
(Dec 17, 2013 12:33 PM)badbrains Wrote: Caching: Use APC
"Minification": Use GZip. GZip > minification
Compiling JS: This is not actually compilation of JS it seems, rather "minification" as you so eloquently put it. GZip would cover this if you install it correctly.
^that...
You shouldn't have to cache, minify or all of that on a project you're hosting on a shared host, as Wolf said.
DO NOT DEAL WITH THIS MEMBER
(Dec 21, 2013 02:15 AM)Mafugsa Bob Wrote: ^that...
You shouldn't have to cache, minify or all of that on a project you're hosting on a shared host, as Wolf said.
But this is not just for shared hosting. If you combine this with GZIP and a cache plugin, it only benefits you.
DO NOT DEAL WITH THIS MEMBER
nice info man . already bookmark :)
Jan 2, 2014 06:14 AM
(This post was last modified: Jan 2, 2014 06:14 AM by Аrthur.)
(Dec 17, 2013 12:25 PM)VladHQ Wrote: [*]Use PDO or mysqli_* instead of mysql_*
I know it's better to use PDO or mysqli these days but in the performance way it doesn't really matter.
http://www.php.net/manual/en/mysqlinfo.api.choosing.php Wrote:It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development, as it has been deprecated as of PHP 5.5.0 and will be removed in the future. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.
Further it's a nice tutorial
DO NOT DEAL WITH THIS MEMBER
Thanks for the feedback, guys, I really appreciate it.
DO NOT DEAL WITH THIS MEMBER
Hate to see this thread go dead, I worked on this for a couple hours.
Cache assets and static files with Nginx and Varnish on frontend with Apache on backend. Optimise Apache configuration. Run an OPCode cacher (or maybe HipHop?). Run a data-store cacher.
|