A simple php extension introduction and development tutorial _ PHP Tutorial

Source: Internet
Author: User
A simple php extension introduction and development tutorial. We use php extension to improve the execution efficiency of the program. for code or logic with a large access volume, we write it as extension. During the project process, we need to sort the data and use php extension to improve the execution efficiency of the program. for code or logic with a large access volume, we need to write it as extension. In the process of project creation, data needs to be sorted, and the data operation is complicated. we are going to sort 1 million pieces of data. Below is a test I made before the program: first, use the php program to generate a random number of 1 million and save it in the file.
The code for generating the number is shown below:

The code is as follows:


Set_time_limit (0 );
Ini_set ("memory_limit",-1 );
$ Data = array ();
For ($ I = 1; I I <1000000; $ I ++)
$ Data [] = rand ();
File_put_contents ('data. php ',' ");


The code is very simple. you can see it at a glance. I will not describe it here.
The following describes the fast sorting functions written in php, the sorting functions in php, and the extended sorting functions. the time required is as follows:

The following shows the php code. it should be noted that the extensions written by yourself when the hello function is used.

The code is as follows:


Ini_set ("memory_limit",-1 );
Set_time_limit (0 );
Include_once ('data. php'); // The generated random number 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 quick sorting time:". ($ s_t-$ s_s )."
";
Echo "the time used by the system ordering function is:". ($ q_t-$ s_t )."
";
Echo "the time used by the local sorting function 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 easy. let's talk about the development of this extension.
There are some steps to generate the extension, which are explained on the Internet, and I will not be so arrogant here,
First, use the php library file to generate a basic extension project.

Generated project,

Modify sort. c and write your own quick sorting. my code here is to change the php program's quick sorting to the c language. After the dll is generated, place it in the php extension. First, check whether the extension is enabled.

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

Note: The extension must be comprehensive and should not cause errors during use, especially the code written in C. Otherwise, the php system will crash.

Bytes. Data needs to be arranged during Project creation...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.