A simple PHP extension introduction and development tutorial

  • 2020-03-31 21:07:06
  • OfStack

The main purpose of using PHP extensions is to make our programs more efficient, and to write them as extensions for highly trafficked code or logic. In the process of doing a project, it is necessary to sort the data, and the data calculation is complex. We're going to sort a million pieces of data, and here's a test I did before the program: first, I used a PHP program to generate a million random Numbers and save them in a file.
The code to generate random Numbers is shown below:
 
set_time_limit(0); 
ini_set("memory_limit", -1); 
$data = array(); 
for($i = 1; $i < 1000000; $i++) 
  $data[] = rand(); 
file_put_contents('data.php', '<?php $data = ' . var_export($data, true) . "; ?>"); 

The code is so simple that it's easy to see.
Here is the quick sort written in PHP, the sort function with PHP itself, and the sort function with its own extension. The time required is as follows:
< img border = 0 SRC = "http://files.jb51.net/upload/201008/20100819220341226.gif" border = 0 >
Let's show you the PHP code. The only thing to note is that the hello function is an extension that you write yourself
 
<?php 
ini_set("memory_limit", -1); 
set_time_limit(0); 
include_once('data.php'); //The random number generated just now is saved in this file
$len = count($data); 
$data_s = $data_q = $data; 
$s_s = $s_t = array_sum(explode(" ", microtime())); 
qsort($data, 0, $len-1); 
$s_t = array_sum(explode(" ", microtime())); 
sort($data_s); 
$q_t = array_sum(explode(" ", microtime())); 
$data_q = hello($data_q); 
$r_t = array_sum(explode(" ", microtime())); 
echo "php Write quicksort time : " . ($s_t-$s_s) . "<br>"; 
echo " The system sort function USES time : " . ($q_t-$s_t) . "<br>"; 
echo " The local sort function takes time : " . ($r_t-$q_t) . "<br>"; 
echo " Comparison of the two results: " . ($data_s === $data_q); 
function qsort(&$arr, $l, $u) 
{ 
if($l >= $u) 
return; 
$m = $l; 
for($i = $l+1; $i<=$u; $i++) 
{ 
if($arr[$i] < $arr[$l]) 
{ 
$m++; 
if($m != $i) 
{ 
$t = $arr[$i]; 
$arr[$i] = $arr[$m]; 
$arr[$m] = $t; 
} 
} 
} 
$t = $arr[$l]; $arr[$l] = $arr[$m]; $arr[$m] = $t; 
qsort($arr, $l, $m-1); 
qsort($arr, $m+1, $u); 
} 
?> 

Very simple, more than that, let's talk about the development of this extension
There are certain steps to generating an extension, which are explained on the web, and I won't get too verbose here,
Start by generating a basic extension project using PHP's library files
< img border = 0 SRC = "http://files.jb51.net/upload/201008/20100819220500559.png" border = 0 >
The generated project,

Change sort. C to include my own quicksort. My code here is to change the quicksort of PHP program to c. Once the DLL is generated, place it in the PHP extension, and first check to see if the extension is enabled.

And then directly use the hello function in the code (I didn't change the function name here)

Note: the extension must be considered in all aspects and should not be used in error, especially the code written in C, otherwise the PHP system will crash

Related articles: