这个扩展早就写好了,只是一直没有时间写在blog上面,今天抽点时间,将它记录下来,以后备用。
我们使用php扩展,美国服务器,主要目的是提高程序的执行效率,香港虚拟主机,对于访问量很大的代码或者逻辑将其写成扩展。在做项目的过程中,需要对数据进行排序,数据运算比较复杂;我们准备对一百万个数据进行排序, 下面是我在程序之前做的一个测试:首先使用php程序生成一百万的随机数,美国服务器,并将其保存在文件中。
生成随即数的代码如下面所示:
复制代码 代码如下:
set_time_limit(0);
ini_set("memory_limit", -1);
$data = array();
for($i = 1; $i $data[] = rand();
file_put_contents('data.php', '");
代码很简单,一看就明白,这里不作说明。
下面是使用php写的快速排序,php自身带的排序函数,以及自己扩展的排序函数,它们所需时间如下所示:
下面将php代码展示一下,需要说明的是:hello函数时自己写的扩展
复制代码 代码如下:
ini_set("memory_limit", -1);
set_time_limit(0);
include_once('data.php'); //刚才产生的随机数保存在这个文件
$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写的快速排序时间: " . ($s_t-$s_s) . "
";
echo "系统排序函数使用时间为: " . ($q_t-$s_t) . "
";
echo "本地排序函数使用时间为: " . ($r_t-$q_t) . "
";
echo "两个结果的对比:" . ($data_s === $data_q);
function qsort(&$arr, $l, $u)
{
if($l >= $u)
return;
$m = $l;
for($i = $l+1; $i{
if($arr[$i] {
$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);
}
?>
很简单吧, 多的不说啦,下面说说这个扩展的开发
生成 扩展有一定的步骤,网上都有很讲解,我这里也不再罗嗦,
首先使用php的库文件生成一个基本的扩展项目
生成的项目,
修改sort.c,将自己的快排写进去, 我这边的代码是将php程序的快速排序改成c语言。生成dll后,将其放在php扩展中,首先检查一下是否已将启用扩展。
然后 在代码中直接使用hello函数(我这里没有改函数名)
注意:扩展一定要考虑全面,不能在使用中出错,特别是C写的代码,否则会出现php系统崩溃

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment