A simple PHP extension introduction and development tutorial_PHP tutorial

WBOY
Release: 2016-07-21 15:35:05
Original
700 people have browsed it

We use PHP extensions, the main purpose is to improve the execution efficiency of the program, and write extensions for codes or logic that have a large number of visits. In the process of doing the project, the data needs to be sorted, and the data operation is relatively complicated; we are going to sort one million pieces of data. Here is a test I did before the program: First, use the PHP program to generate one million random numbers. number and save it in a file.
The code to generate a random number is as follows:

Copy the code The code is as follows:

set_time_limit(0);
ini_set("memory_limit", -1);
$data = array();
for($i = 1; $i < 1000000; $i++)
 $data[] = rand();
file_put_contents('data.php', '");

The code is very simple and can be understood at a glance, so I won’t explain it here.
The following is quick sort written in PHP, PHP's own sorting function, and its own extended sorting function. The time required for them is as follows:
A simple PHP extension introduction and development tutorial_PHP tutorial
The PHP code is shown below. It should be noted that the hello function is an extension written by myself
Copy the code The code is as follows:

ini_set("memory_limit", -1);
set_time_limit(0);
include_once('data.php'); //The random number just generated 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 "Quick sort time written in php: " . ($s_t- $s_s) . "
";
echo "The system sorting function usage time is: " . ($q_t-$s_t) . "
";
echo "The local sorting function usage time is: " . ($r_t-$q_t) . "
";
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);
}
?>

It’s very simple. I won’t go into too much detail. Let’s talk about the development of this extension.
There are certain steps to generate an extension. There are many explanations on the Internet. I won’t go into too much detail here.
First use the PHP library file to generate a basic extension project
A simple PHP extension introduction and development tutorial_PHP tutorial
The generated project,

modify sort.c and write your own quick sort into it, my code here It is to change the quick sort of PHP program into C language. After generating the dll, put it in the php extension, first check if the extension has been enabled.

Then use the hello function directly in the code (I have not changed the function name here)

Note: Extensions must be considered comprehensively and no mistakes can be made during use, especially code written in C, otherwise There will be a php system crash

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322286.htmlTechArticleWe use php extensions. The main purpose is to improve the execution efficiency of the program. For codes or logic with a large number of visits, It is written as extension. In the process of working on the project, the data needs to be sorted...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template