<p>What can I do to increase the performance/speed of my PHP scripts without installing software on my servers?</p>

[{'answer_id': 12579, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 2, 'selected': False, 'text': '<p>There is no magic solution, and attempting to provide generic solutions could well just be a waste of time.</p>\n\n<p>Where are your bottlenecks? For example are your scripts processor/database/memory intensive?</p>\n\n<p>Have you performed any profiling?</p>\n'}, {'answer_id': 12580, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 0, 'selected': False, 'text': '<p>Whenever I look at performance problems, I think the best thing to do is time how long your pages take to run, and then look at the slowest ones.  When you get these real metrics, you can often improve performance on the slowest ones by orders of magnitude, either by fixing a slow SQL query or perhaps tightening up the code a bit.</p>\n\n<p>This of course requires no new hardware or special software, just a critical eye on the existing code.</p>\n\n<p>That said, this will only work for so long... if you really are getting enough traffic to hit the limits of your hardware, and/or there is some code that is just inherently slow and really required, you will have to look at other possibilities.</p>\n'}, {'answer_id': 12584, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 5, 'selected': True, 'text': "<p>Profile. Profile. Profile. I'm not sure if there is anything out there for PHP, but it should be simple to write a little tool to insert profiling information in your code. You will want to profile function times and SQL query times.</p>\n\n<p>So where you have a function:</p>\n\n<pre><code>function foo($stuff) {\n    ...\n    return ...;\n}\n</code></pre>\n\n<p>I would change it to:</p>\n\n<pre><code>function foo($stuff) {\n    trace_push_fn('foo');\n    ...\n    trace_pop_fn('foo');\n    return ...;\n}\n</code></pre>\n\n<p>(This is one of those cases where multiple returns in a function become a hinderance.)</p>\n\n<p>And SQL:</p>\n\n<pre><code>function bar($stuff) {\n    trace_push_fn('bar');\n\n    $query = ...;\n    trace_push_sql($query);\n    mysql_query($query);\n    trace_pop_sql($query);\n\n    trace_pop_fn('bar');\n    return ...;\n}\n</code></pre>\n\n<p>In the end, you can generate a full trace of the program execution and use all sorts of techniques to identify your bottlenecks.</p>\n"}, {'answer_id': 12857, 'author': 'Eric Goodwin', 'author_id': 1430, 'author_profile': 'https://Stackoverflow.com/users/1430', 'pm_score': 0, 'selected': False, 'text': "<p>I'm responsible for a large reporting system and we track the slowest reports kind of like that.  I fire a unique key into the db when the report starts and then when it finishes I can determine how long it took.  I'm using the database because that way I can detect when pages timeout (which happens a lot more than I'd like)</p>\n"}, {'answer_id': 12859, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 3, 'selected': False, 'text': '<p>One reasonable technique that can easily be pulled off the shelf is caching. A vast amount of time tends to go into generating resources for clients that are common between requests (and even across clients); eliminating this runtime work can lead to dramatic speed increases. You can dump the generated resource (or resource fragment) into a file outside the web tree, and then read it back in when needed. Obviously, some profiling will be needed to ensure this is actually faster than regeneration - forcing the web server back to disk regularly can be detrimental, so the resource really does need to have heavy reuse.</p>\n\n<p>You might also be surprised how much time is spent inside badly written database queries; time common generated queries and see if they can be rewritten. The amount of time spent executing actual PHP code is generally pretty limited, unless you\'re using some sub-optimal algorithms. </p>\n\n<p>Neither of these are limited to PHP, though some of the PHP "magicy" approaches/functions can over-protect one from thinking about these concerns. For example, I recently updated a script that was using array_search to use a binary search over a sorted array, and gained the expected exponential speedup.</p>\n'}, {'answer_id': 12868, 'author': 'Barrett Conrad', 'author_id': 1227, 'author_profile': 'https://Stackoverflow.com/users/1227', 'pm_score': 0, 'selected': False, 'text': '<p>Follow some of the other advice first like profiling and making good resource allocation decisions, e.g. caching.  </p>\n\n<p>Also, take into account the performance of outside resources like your database.  In MySQL you can check the <a href="http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html" rel="nofollow noreferrer">slow query log</a> for example.  In addition make sure you didn\'t design your database an forget about it.  Optimizing your queries (<a href="http://dev.mysql.com/doc/refman/5.0/en/using-explain.html" rel="nofollow noreferrer">again for MySQL</a>) against real data can pay of big.</p>\n'}, {'answer_id': 16348, 'author': 'Dave Marshall', 'author_id': 1248, 'author_profile': 'https://Stackoverflow.com/users/1248', 'pm_score': 1, 'selected': False, 'text': '<p>The ones I can think of...</p>\n\n<ul>\n<li><p><a href="http://hudzilla.org/phpwiki/index.php?title=Get_your_loops_right_first" rel="nofollow noreferrer">Loop invariants</a> are always a\ngood one to watch.</p></li>\n<li><p>Write E_STRICT and E_NOTICE\ncompliant code, particularly if you\nare logging errors.</p></li>\n<li><p>Avoid the @ operator.</p></li>\n<li><p>Absolute paths for requires and\nincludes.</p></li>\n<li><p>Use strpos, str_replace etc. instead of regular expressions whenever possible.</p></li>\n</ul>\n\n<p>Then there\'s a bunch of other methods that might work, but probably wont give you much benefit.</p>\n'}, {'answer_id': 87954, 'author': 'Willem', 'author_id': 15447, 'author_profile': 'https://Stackoverflow.com/users/15447', 'pm_score': 3, 'selected': False, 'text': '<p>Really consider using XDebug profiler: it helps with checking how much a certain function is being executed against what you would have expected.</p>\n\n<p>I try to decrease instructions while improving code readability by replacing logic with array-lookups when appropriate. \nIt\'s what Jeff Atwood wrote in [The Best Code is No Code At All][1].</p>\n\n<ul>\n<li>Also, avoid loops inside another\nloop, and nested if/else statements.</li>\n<li>Short functions.  Sometimes a lot of\ncode does not need to be executed\nwhen the result-value is already\nknown. </li>\n<li><p>Unnecessary testing:</p>\n\n<blockquote>\n  <p>if (count($array) === 0) return;</p>\n</blockquote>\n\n<p>can also be written as:</p>\n\n<blockquote>\n  <p>if (! $array) return;</p>\n</blockquote>\n\n<p>Another function-call eliminated!</p>\n\n<p>[1]: <a href="http://www.codinghorror.com/blog/archives/000878.html" rel="noreferrer">http://www.codinghorror.com/blog/archives/000878.html</a>"The Best Code is No Code At All"</p></li>\n</ul>\n'}, {'answer_id': 88051, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>including files is slow, and requiring them is even slower. If you use __autoload for including every class then that will add up. for example.</p>\n\n<p>I'm always a bit wary of trying to be too clever in terms of code optimisation, if it sacrifices code clairty. If you need to make code obscure to make it fast, would it not be cheaper to upgrade hardwear instead of wasting your time trying to tweak code? Processor cycles are cheaper than programmer cycles, after all.</p>\n"}, {'answer_id': 88443, 'author': 'TobiX', 'author_id': 13258, 'author_profile': 'https://Stackoverflow.com/users/13258', 'pm_score': 0, 'selected': False, 'text': '<p>Rasmus Lerdorf gave some good tips in his recent presentation "<a href="http://talks.php.net/show/froscon08" rel="nofollow noreferrer">Simple is Hard</a>" at FrOSCon \'08. If you are using a bytecode cache (and you really should be using one), include path misses hurt a lot, so optimize your require/require_once.</p>\n'}, {'answer_id': 18545783, 'author': '0xBAADF00D', 'author_id': 436766, 'author_profile': 'https://Stackoverflow.com/users/436766', 'pm_score': 0, 'selected': False, 'text': '<p>You can use profiling tool like xhprof to view what part of your code can by optimized !</p>\n'}, {'answer_id': 24074405, 'author': 'Dinesh Saini', 'author_id': 1547664, 'author_profile': 'https://Stackoverflow.com/users/1547664', 'pm_score': 3, 'selected': False, 'text': '<p>You can optimized the code with two basic things:</p>\n\n<h2>Optimizing PHP associated library and server</h2>\n\n<p>Go through <a href="https://www.digitalocean.com/community/articles/how-to-optimize-apache-web-server-performance">https://www.digitalocean.com/community/articles/how-to-optimize-apache-web-server-performance</a> Or</p>\n\n<p>You can use profiling tool like xhprof to view what part of your code can by optimized and here is the link to follow: <a href="http://michaelsanford.com/compiling-xhprof-for-php-5-4/">http://michaelsanford.com/compiling-xhprof-for-php-5-4/</a></p>\n\n<h2>Optimizing your code using code profiler and code analyzer</h2>\n\n<p>You need to install Netbeans in order to use this plugin. \nHere are the steps you need to follow:</p>\n\n<p>1) Open NetBeans then select option from menu bar Tools > Plugin. Then search plug-in name "phpcsmd" in the available plug-in tab and install it from there.</p>\n\n<p>2) Now open the terminal and be there as the super user by typing command "sudo su".</p>\n\n<p>3) Install PEAR library (if it is not installed) into your system by running following commands into your terminal</p>\n\n<pre><code>a) wget http://pear.php.net/go-pear.phar\nb) php go-pear.phar\n</code></pre>\n\n<p>As we need this for the installation of further addons.</p>\n\n<p>4) Then run the command</p>\n\n<pre><code>"pear config-set auto_discover 1"\n</code></pre>\n\n<p>This will be used to set auto discover the path "true" for the required plug-ins. So they get install to the desired location automatically.</p>\n\n<p>5) Then run below command to install PHP code sniffer.</p>\n\n<pre><code>"pear install --alldeps pear/PHP_CodeSniffer"\n</code></pre>\n\n<p>6) Now to install the PHP Mess Detector by running following command</p>\n\n<pre><code>"pear install --alldeps phpmd/PHP_PMD"\n</code></pre>\n\n<p>If you get the error like "invalid package name/package file "phpmd/PHP_PMD"" while installing this module. You need to use this "pear channel-discover pear.phpmd.org" command to get rid of this error. After this command you can run the above command again to install Mess detector.</p>\n\n<p>7) Now to install the PHP Depend by running following command</p>\n\n<pre><code>"pear install --alldeps pdepend/PHP_Depend"\n</code></pre>\n\n<p>8) Now install the PHP Copy Paste Detector by running following command</p>\n\n<pre><code>"pear install --alldeps phpunit/phpcpd"\n</code></pre>\n\n<p>9) Then run the command</p>\n\n<pre><code>"pear config-set auto_discover 0"\n</code></pre>\n\n<p>This will be used to set auto discover path "false".</p>\n\n<p>10) Then open net beans and follow the path Tools>Options>PHP>PHPCSMD</p>\n'}, {'answer_id': 47991499, 'author': 'Stephan Muggli', 'author_id': 7459505, 'author_profile': 'https://Stackoverflow.com/users/7459505', 'pm_score': 0, 'selected': False, 'text': '<p>1) Use latest version of PHP</p>\n\n<p>The core team is working hard on improving the performance of PHP in every version.</p>\n\n<p>2) Use a bytecode cache</p>\n\n<p>Since PHP 5.5 a bytecode cache has been added to PHP named <code>OPcache</code>. Using <code>OPcache</code> can make a huge difference especially since PHP 7. It receives improvements in every PHP version and might even get a <a href="https://externals.io/message/95531" rel="nofollow noreferrer">JIT implementation in the future</a>.</p>\n\n<p>3) Profiling</p>\n\n<p>While developing profiling gives you great insight what exactly is happening. This helps a lot finding bottlenecks in your code.</p>\n\n<p>One of the most used tools is <code>XHProf</code> but is not officially supported anymore and has issues with PHP >= 7. An alternative when you want to profile PHP >= 7 is <a href="https://tideways.io/profiler/xhprof-for-php7-php5.6" rel="nofollow noreferrer"><code>Tideways</code> which is a fork of <code>XHProf</code></a>.</p>\n'}]
['https://Stackoverflow.com/questions/12576', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']