Optimizing PHP code

This article is intended to help you to optimize your PHP code, it will address various examples showing and comparing the time between generations of different possible solutions.


Index :
Loops : For and While
Assigning variables: 'x' and "x"
Reading a text file: file_get_contents and fopen-fget
Infuance variables in the code


Loops: for and while
Test code for loops:
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 500001;
$time_start microtime_float();

for(
$i=0;$i<$nombre_boucle;$i++)
{
}

$time_end microtime_float();
$duration_for_loop $time_end $time_start;)
?>


Test code while loops:
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 500001;
$i 0;
$boucle_active true;

$time_start microtime_float();

while (
$boucle_active)
{
    if(
$i>=$nombre_boucle){$boucle_active=false;}
    
$i++;
}

$time_end microtime_float();
$duration_while_loop $time_end $time_start;
?>


Results for 500000 loops of 5 tests:
For loop:
0,0396s
0,0396s
0,0404s
0,0407s
0,0386s
Mean = 0,0398s

While loop:
0,0443s
0,0435s
0,0441s
0,0446s
0,0439s
Mean = 0,0441s

Conclusion :
For loop is 10% faster than the While loop in this example.

Back to index


Assigning variables: 'x' and 'x':

Code testing variable 'x':
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 500001;
$time_start microtime_float();

for(
$i=0;$i<$nombre_boucle;$i++)
{
 
$ma_variable 'blablabla';
}

$time_end microtime_float();
$duration_variable_01 $time_end $time_start $duration_for_loop;
?>


Code testing variable "x":
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 500001;
$time_start microtime_float();

for(
$i=0;$i<$nombre_boucle;$i++)
{
 
$ma_variable "blablabla";
}

$time_end microtime_float();
$duration_variable_02 $time_end $time_start $duration_for_loop;
?>


Results for 500000 loops of 5 tests:
Assignment of variables 'x':
0,0436s
0,0462s
0,0424s
0,0418s
0,0459s
Mean = 0,0440s

Assignment of variables "x":
0,0452s
0,0435s
0,0415s
0,0419s
0,0466s
Mean = 0,0438s

Conclusion :
The assignment of variables "x" in a performance equivalent to the assignment of variables 'x'.

Back to index


Reading a text file: file_get_contents and fopen-fget:
This test will be conducted in 2 parts: reading a text file of one line and reading a text file of 10 lines.

Test code for reading a text file with a line file_get_contents:
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 10000;
$time_start microtime_float();
$nom_fichier "montexte.txt";

for(
$i=0;$i<$nombre_boucle;$i++)
{
 
$ma_variable file_get_contents($nom_fichier);
}

$time_end microtime_float();
$duration_file_get_contents $time_end $time_start $duration_for_loop;
?>


Test code for reading a text file with a line-fopen fget:
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 10000;
$ma_variable "";
$time_start microtime_float();
$nom_fichier "montexte.txt";

for(
$i=0;$i<$nombre_boucle;$i++)
{
  
$fp fopen($nom_fichier,"r");
  while(!
feof($fp))
  {
      
$ma_variable .= fgets($fp,4096) . "\n";
  }
  
fclose($fp);
}

$time_end microtime_float();
$duration_fopen $time_end $time_start $duration_for_loop;
?>


Test code for reading a text file of 10 lines with file_get_contents:
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 10000;
$time_start microtime_float();
$nom_fichier "montexte10.txt";

for(
$i=0;$i<$nombre_boucle;$i++)
{
 
$ma_variable file_get_contents($nom_fichier);
}

$time_end microtime_float();
$duration_file_get_contents_all $time_end $time_start $duration_for_loop;
?>


Test code for reading a text file of 10 lines with fopen-fget :
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 10000;
$ma_variable "";
$time_start microtime_float();
$nom_fichier "montexte10.txt";

for(
$i=0;$i<$nombre_boucle;$i++)
{
  
$fp fopen($nom_fichier,"r");
  while(!
feof($fp))
  {
      
$ma_variable .= fgets($fp,4096) . "\n";
  }
  
fclose($fp);
}

$time_end microtime_float();
$duration_fopen_all $time_end $time_start $duration_for_loop;
?>


Results for 10000 loops of 5 tests:
Reading a text file of one line with file_get_contents:
0,5492s
0,5503s
0,5395s
0,5420s
0,5610s
Mean = 0,5484s

Reading a text file of one line with fopen-fget :
0,5131s
0,5070s
0,5063s
0,5039s
0,5162s
Mean = 0,5093s

Reading a text file of 10 lines with file_get_contents:
0,5525s
0,5466s
0,5499s
0,5463s
0,5559s
Mean = 0,5502s

Reading a text file of 10 lines with fopen-fget:
0,6697s
0,6527s
0,6522s
0,6523s
0,6530s
Mean = 0,6560s

Conclusion :
When reading a text file of one line, the combination fopen-fget is more efficient than 7% as file_get_contents but when reading a text file of 10 lines, file_get_contents is more efficient than 16% as the combination function fopen-fget.

Back to index


Infuance variables in the code:
This test is to see the difference between the definition of several cons a single variable to get the same result.

Code multivariate:
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 500001;

$time_start microtime_float();

for(
$i=0;$i<$nombre_boucle;$i++)
{
    
$val01 5;
    
$val02 3;
    
$val03 6;
    
$val04 2;
    
$val05 9;
    
    
$val06 $val01*$val02;
    
$val07 $val06/$val03;
    
$val08 $val07^$val05;
    
$val09 $val08/$val04;
    
$val10 $val09^$val07;
}

$time_end microtime_float();
$duration_val01 $time_end $time_start;
?>


Test code to a variable:
function microtime_float(){
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);


$nombre_boucle 500001;

$time_start microtime_float();

for(
$i=0;$i<$nombre_boucle;$i++)
{
    
$val10 = (((5*3/6)^9)/2)^(5*3/6);
}

$time_end microtime_float();
$duration_val01 $time_end $time_start;
?>


Results for 50000 loops of 5 tests:
Multivariate Testing:
0,2962s
0,2921s
0,2943s
0,2985s
0,2941s
Mean = 0,2950s

Testing a variable:
0,1650s
0,1621s
0,1653s
0,1659s
0,1660s
Mean = 0,1648s

Conclusion :
The test shows a huge loss of performance when using the use of several variables, calculating a value in a single variable is faster than 44%.

Back to index




Leave comment ?
Name :
 

Email :
 

Comment :
 

Gatcha :

 

[ Send ]


0.0407s