A simple php extension introduction and development tutorial

Source: Internet
Author: User
Tags php write
This extension has been written for a long time, but it has never been written on the blog. Today, we will take some time to record it and use it later.

This extension has been written for a long time, but it has never been written on the blog. Today, we will take some time to record it and use it later.

We use php extension, an American server, to improve program execution efficiency. The Hong Kong virtual host writes the code or logic that has a large access volume to extend it. 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, the U.S. server, 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.

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.