This is a discussion on Website Performance Tips & Tricks within the Web Design Help forums, part of the Web Development category; Generic Session Optimizations • Don't use session.auto_start. • Do not enable session.use_trans_sid • Whenever possible set session.cache_limiter to private_no_expire • ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Generic Session Optimizations • Don't use session.auto_start. • Do not enable session.use_trans_sid • Whenever possible set session.cache_limiter to private_no_expire • Assign each user (vhost) it's own sessions directory. • For large sites consider using session.save_path = "N;/path" • If possible avoid automatic garbage collection.
__________________ J.Vijayanand |
| Sponsored Links |
| |||
| SSL Acceleration : Most of the E-commerce site enabled a SSL for Credit card offload encryption/decryption prcoessing task.The Web servers to spend more processing on content genratin and delivery.When considering an SSL accelerator, take into consideration the number of transactions per second (TPS) it can handle. TPS will be more important on SSL Acceleration appliances than cards. For instance, if you buy an SSL Accelerator Card that can handle 2000 TPS for a server that can only handle 500 TPS, then you waste the money on the 1500 TPS you can't use. An SSL Acceleration hardware appliance (box) can deal with transactions for all your web servers and scale quickly.Thanks R.Rajan |
| |||
| we can increase the performance of the site by using following steps: 1) for ($i=0;$i<count($some_array);++$i) { }is slow $temp=count($some_array); for ($i=0;$i<$temp;++$i) { }is faster for ($i=0;isset($some_array[$i]);++$i) { }is fastest 2) Use ++$i instead of $i++. The second one uses a temporary copy of $i to add 1 to, then assign to $i.
__________________ J.Vijayanand |
| |||
| Hi, Using 'echo' is faster than 'print' because it is a language construct, not a function. and also using include ‘/some/file.php’ instead of include(’/some/file.php’). Same for require and the _once equivalents. These will lead to increase the performance of the site.
__________________ J.Vijayanand |
| |||
| The places where such high returns are achievable are in the while and for loops that litter our code, where each slowdown in the code is magnified by the number of times we iterate over them. The best way of understanding what can be optimized is to use a few examples: Example 1 Here is one simple example that prints an array: for ($j=0; $j<sizeof($arr); $j++) echo $arr[$j]."<br>"; This can be substantially speeded up by changing the code to: for ($j=0, $max = sizeof($arr), $s = ''; $j<$max; $j++) $s .= $arr[$j]."<br>"; echo $s; First we need to understand that the expression $j<sizeof($arr) is evaluated within the loop multiple times. As sizeof($arr) is actually a constant (invariant), we move the cache the sizeof($arr) in the $max variable. In technical terms, this is called loop invariant optimization. The second issue is that in PHP 4, echoing multiple times is slower than storing everything in a string and echoing it in one call. This is because echo is an expensive operation that could involve sending TCP/IP packets to a HTTP client. Of course accumulating the string in $s has some scalability issues as it will use up more memory, so you can see a trade-off is involved here. An alternate way of speeding the above code would be to use output buffering. This will accumulate the output string internally, and send the output in one shot at the end of the script. This reduces networking overhead substantially at the cost of more memory and an increase in latency. In some of my code consisting entirely of echo statements, performance improvements of 15% have been observed. ob_start(); for ($j=0, $max = sizeof($arr), $s = ''; $j<$max; $j++) echo $arr[$j]."<br>"; Note that output buffering with ob_start() can be used as a global optimization for all PHP scripts. In long-running scripts, you will also want to flush the output buffer periodically so that some feedback is sent to the HTTP client. This can be done with ob_end_flush(). This function also turns off output buffering, so you might want to call ob_start() again immediately after the flush. In summary, this example has shown us how to optimize loop invariants and how to use output buffering to speed up our code. Example 2 In the following code, we iterate through a PEAR DB recordset, using a special formatting function to format a row, and then we echo the results. This time, I benchmarked the execution time at 10.2 ms (this excludes the database connection and SQL execution time): function FormatRow(&$recordSet) { $arr = $recordSet->fetchRow(); return '<b>'.$arr[0].'</b><i>'.$arr[1].'</i>'; } for ($j = 0; $j < $rs->numRows(); $j++) { print FormatRow($rs); } From example 1, we learnt that we can optimize the code by changing the code to the following (execution time: 8.7 ms): function FormatRow(&$recordSet) { $arr = $recordSet->fetchRow(); return '<b>'.$arr[0].'</b><i>'.$arr[1].'</i>'; } ob_start(); for ($j = 0, $max = $rs->numRows(); $j < $max; $j++) { print FormatRow($rs); } My benchmarks showed me that the use of $max contributed 0.5 ms and ob_start contributed 1 ms to the 1.5 ms speedup. However by changing the looping algorithm we can simplify and speed up the code. In this case, execution time is reduced to 8.5 ms: function FormatRow($arr) { return '<b>'.$arr[0].'</b><i>'.$arr[1].</i>'; } ob_start(); while ($arr = $rs->fetchRow()) { print FormatRow($arr); } One last optimization is possible here. We can remove the overhead of the function call (potentially sacrificing maintainability for speed) to shave off another 0.1 milliseconds (execution time: 8.4 ms): ob_start(); while ($arr = $rs->fetchRow()) { print '<b>'.$arr[0].'</b><i>'.$arr[1].'</i>'; } By switching to PEAR Cache, execution time dropped again to 3.5 ms for cached data: require_once("Cache/Output.php"); ob_start(); $cache = new Cache_Output("file", array("cache_dir" => "cache/") ); $t = getmicrotime(); if ($contents = $cache->start(md5("this is a unique kexy!"))) { print "<p>Cache Hit</p>"; print $contents; } else { print "<p>Cache Miss</p>"; ## ## Code to connect and query database omitted ## while ($arr = $rs->fetchRow()) { print '<b>'.$arr[0].'</b><i>'.$arr[1].'</i>'; } print $cache->end(100); } print (getmicrotime()-$t); We summarize the optimization methods below: ExecutionTime (ms) Optimization Method 9.9 Initial code, no optimizations, excluding database connection and SQL execution times. 9.2 Using ob_start 8.7 Optimizing loop invariants ($max) and using ob_start 8.5 Changing from for-loop to while-loop, and passing an array to FormatRow()and using ob_start 8.4 Removing FormatRow()and using ob_start 3.5 Using PEAR Cache and using ob_start From the above figures, you can see that biggest speed improvements are derived not from tweaking the code, but by simple global optimizations such as ob_start(), or using radically different algorithms such as HTML caching. |
| |||
| Hey all, due to the very good performance of this thread, ive made this thread as sticky and have moved it to a more appropriate forum. Thanks
__________________ Vinoth Chandar Creator of Discussweb |
| |||
| Use HTML caching if you have data that rarely changes. Even if the data changes every minute, caching can help provided the data is synchronized with the cache. Depending on your code complexity, it can improve your performance by a factor of 10. Thanks, Ramkumar.B |
| |||
| Beginner Vs Experienced Beginner : echo "$var"; Experienced : echo $var; Beginner:echo "<a href="http://www.php.net">PHP</a>"; Experienced : ?><a href="http://www.php.net">PHP</a><? ... ?> Beginner : $a[0]=1; $a[1]=2; $a[2]=3; Experienced : $a = array(1,2,3, 1); Beginner : if($a>1) { $b=2; } else { $b=3; } Experienced : $b = ($a>1) ? 2:3; Beginner : $result=mysql_query(...); Experienced: $result=mysql_query(...) OR die (mysql_error()); Where Experienced reduces the time of execution and coding length.
__________________ J.Vijayanand |
| |||
| We can speed up the site by means of following... If you use Javascript functions or STYLE tags, then put all of these into a file called common.inc and use the following: <? require 'common.inc'; ?> on any page that you want these function included.
__________________ J.Vijayanand |
| |||
| The fastest way to concatenate multiple small strings into one large string is to create an output buffer (ob_start) and to echo into the buffer. At the end get the contents using ob_get_contents. This works because memory allocation is normally the killer in string concatenation, and output buffering allocates a large 40K initial buffer that grows in 10K chunks Thanks, Ramkumar.B |
| |||
| For better performance of the site, you might want to disable calling fsync(). (There is risk that you loose more data when system is crushed, though) To disable auto fsync(), start postmaster like postmaster -o '-F' This is recommended setting for session database, since session database is not critical as account database and it accessed most often.
__________________ J.Vijayanand Last edited by Booom : 08-21-2007 at 02:11 AM. |
| |||
| Better php.ini setting (track_vars, register_globals) PHP users are better to set enable "track_vars" and disable "register_globals" for sevral reasons.Thanks -Kathir |
| |||
| Content Delivery Networks
Basically, they have multiple servers across the world and when a request comes for yourcompany.com for example, the CDN "owns" the DNS for that name and static .html, JavaScript files, and image files can be served from their edge server. If their edge server doesns't contain your .html, image, or JavaScript, their edge server sends a request back to your web server transparently to another domain (another.yourcompany.com) on the internet to retrieve the files. The files then get sent back to their edge server and stored there and then sent to the client. If your file is at their edge server, the client machine (requestor) gets the files from their edge server. Thus if your web server is sitting in San Francisco and someone from London wants to visit your web site, your site content can be served from a CDN server in London instead of San Francisco, reducing response time. Obviously, the time it takes for data to transfer within London is a lot less than from London to San Francisco. CDNs can also provide live streaming functionality - serving bandwidth intensive media from their content servers... something that can be costly if you are a multimedia-rich company. Watch out for testing results CDN companies give that are irrelevant to your site. Any test results of downloading large single 300kb .gif files are not indicative of how well a CDN's service is appropriate to you.
__________________ J.Vijayanand Last edited by vijayanand : 08-15-2007 at 11:54 PM. Reason: alignment |
| |||
| Two types of performance
__________________ J.Vijayanand |
| |||
| Performance Tip:
__________________ J.Vijayanand Last edited by vijayanand : 08-16-2007 at 11:19 PM. |
| |||
| Problems of style It's a matter of style and convenience to produce scripts in such a way that make them easy to read and debug. If you are using a programming editor that highlights your code it will be easy to identify the various parts. This may explain why you find syntax that looks rather confusing at first. Some examples: $line = $result['name'] . ' ' . $result['last_name']; // ok - easy to read/debug $line = $result["name"] . ' ' . $result["last_name"]; // ok, but why use double quotes if they are not necessary? $line = "$result[name] $result[last_name]"; // ok - but much harder to read/debug - poor coding style $line = $result['name'] . ' ' . doSomething($result['last_name']); // ok - preferred method (using a function) If you are working with any kind of a team and/or plan on allowing others access to your work in the future it's etiquette to try to make it accessible and easy on the eyes.
__________________ J.Vijayanand |
| |||
| Turn On Error Reporting Immediately The single most important thing I tell people who use PHP is to turn error reporting to its maximum level. Why would I want to do this? Generally the error reporting is set at a level that will hide many little things like: * declaring a variable ahead of time, * referencing a variable that isn’t available in that segment of code, or * using a define that isn’t set. These factors might not seem like that big a deal -- until you develop structured or object oriented programs with functions and classes. Too often, writing code with the error reporting turned up high would cost your hours as you scoured long functions that didn’t work because a variable was misspelled or not accessible. PHP won’t tell you anything in that case – it’ll just create the new variable for you and initialize it to zero. The remedy is to put the following line at the top of every PHP document as you develop: error_reporting(E_ALL); It simply forces the error reporting to be at its highest level. Try putting this line in other PHP programs, and more often than not you’ll receive a barrage of warning messages that identify all the potentially wrong elements of the code. *Note - The error report should be off while in production server to avoid users to not view the error.
__________________ J.Vijayanand |
| |||
| Combining PHP classes Out of sheer boredom I wanted to see which was better, including one big file of classes or splitting your classes up into multiple files. I'm going to use nusoap as an example. The NuSoap package lets you download one file. nusoap.php. That one file contains 9 classes. While not only is this method nice as you only need to include one class, its also 24% faster on average than having to include 9 seperate files.Thanks -Kathir |
| |||
| Optimize Your HTML Code Try to use few tables or divide a big one into several small ones because your user will see the content only after the whole table is built. (So put e.g. the title and subtitle in one small table and the rest in a bigger table: the small table will be visible before the bigger one.)Thanks -Kathir |