PDA

View Full Version : PHP Optimization



oxygenws
چهارشنبه 27 اسفند 1382, 00:05 صبح
سلام،

این مطلب رو از تو یک کتاب آوردم، به نظرم خیلی جالب اومد.... ترجیح می دم خودم ساکت بشم و خودتون مطالب انگلیسی این مطلب رو بخونید، خودش بهتر از من توضیح داده....


Avoiding bad loops
The most common PHP problem with speed comes from badly written loops. Listing
21-3 shows a PHP script that demonstrates both inefficiently written loops and efficiently
written loops.

Listing 21-3: loops.php
<?php
// If you have installed PEAR packages in a different
// directory than %DocumentRoot%/pear change
// the setting below.
$PEAR_DIR = $_SERVER[‘DOCUMENT_ROOT’] . ‘/pear’ ;
$PATH = $PEAR_DIR;
ini_set( ‘include_path’, ‘:’ . $PATH . ‘:’ .
ini_get(‘include_path’));
require_once ‘Benchmark/Iterate.php’;
define(MAX_RUN, 100);
$data = array(1, 2, 3, 4, 5);
doBenchmark(‘v1’, $data);
doBenchmark(‘v2’, $data);
doBenchmark(‘v3’, $data);
doBenchmark(‘v4’, $data);
function doBenchmark($functionName = null, $arr = null)
{
reset($arr);
$benchmark = new Benchmark_Iterate;
$benchmark->run(MAX_RUN, $functionName, $arr);
$result = $benchmark->get();
echo ‘<br>’;
printf(“%s ran %d times where average exec time %.5f ms”,
$functionName,
$result[‘iterations’],
$result[‘mean’] * 1000);
}
function v1($myArray = null) {
// Do bad loop
for ($i =0; $i < sizeof($myArray); $i++)
{
echo ‘<!--’ . $myArray[$i] . ‘ --> ‘;
}
}
function v2($myArray = null) {
// Do better loop
// Get the size of array
$max = sizeof($myArray);
for ($i =0; $i < $max ; $i++)
{
echo ‘<!--’ . $myArray[$i] . ‘ --> ‘;
}
}
function v3($myArray = null) {
// Do much better loop
// Get the size of array
$max = sizeof($myArray);
for ($i =0; $i < $max ; $i++)
{
// Store the output in a string
$output .= ‘<!--’ . $myArray[$i] . ‘ --> ‘;
}
// Echo the output string
echo $output;
}
function v4($myArray = null) {
// Do much better loop
// Get the size of array
$max = sizeof($myArray);
$output = array();
for ($i =0; $i < $max ; $i++)
{
// Store the output in a string
array_push($output, ‘<!--’ .
$myArray[$i] .
‘ --> ‘
);
}
// Echo the output string
echo implode(‘’, $output);
}
?>
This script demonstrates four versions of a method (v1, v2, v3, and v4), each of
which performs the same task, but with progressive efficiency.
The script calls a method called doBenchmark(), which runs each version of the
function under benchmark conditions using the Benchmark/Iterate.php class for
MAX_RUN times. The result of each benchmark is printed using a printf() statement.
Following is a sample of output from the script:
v1 ran 100 times where average exec time 1.60087 ms
v2 ran 100 times where average exec time 1.05392 ms
v3 ran 100 times where average exec time 0.55139 ms
v4 ran 100 times where average exec time 0.27371 ms

موفق باشید، امید

delphi77
دوشنبه 16 دی 1387, 14:02 عصر
خیلی هم ساکت نشین، یک توضیح هم هرچند کوچک بد نیست این تاپیک فارسیه نه!

yaqubian
دوشنبه 16 دی 1387, 14:57 عصر
دوست عزیز
oxygenws (http://barnamenevis.org/forum/member.php?u=4661) خیلی وقته که در این تالار فعالیت نمی کنن! شما خودتون دست به کار شین!
موفق باشید