php 多线程
2016-01-29 18:43
621 查看
昨天做了个题目:http://blog.csdn.net/liujihaozhy/article/details/50599388
一筐鸡蛋:
1个1个拿,正好拿完。
2个2个拿,还剩1个。
3个3个拿,正好拿完。
4个4个拿,还剩1个。
5个5个拿,还剩4个。
6个6个拿,还剩3个。
7个7个拿,正好拿完。
8个8个拿,还剩1个。
9个9个拿,正好拿完。
问筐里有多少鸡蛋?
求高人解答???
在本地计算这个过程,竟然超过了2秒的时间,突然想到多线程这个概念,于是就安装了pthread这个扩展,官网代文档说明:http://php.net/manual/zh/book.pthreads.php,然后跑到张宴的博客http://zyan.cc/pthreads/,根据提示安装成功,
下面就来缩短上面的计算时间(引用大神的代码进行修改):
//thread class
class test_thread_run extends Thread {
public $data;
public $start;
public $end;
public function __construct($start,$end) {
$this->start = $start;
$this->end = $end;
}
public function run() {
$this->data = get_data ($this->start,$this->end);
}
}
function model_thread_result_get($urls_array) {
foreach ( $urls_array as $key => $value ) {
$thread_array [$key] = new test_thread_run ($value['start'],$value['end']);
$thread_array [$key]->start ();
}
foreach ( $thread_array as $thread_array_key => $thread_array_value ) {
while ( $thread_array [$thread_array_key]->isRunning () ) {
usleep ( 10 );
}
if ($thread_array [$thread_array_key]->join ()) {
$variable_data [$thread_array_key] = $thread_array [$thread_array_key]->data;
}
}
return $variable_data;
}
function get_data($start,$end){
$return = array();
for ($i=$start;$i<$end;$i++){
if(!($i%1) && ($i%2 == 1)
&& !($i%3) && ($i%4 == 1)
&& ($i%5 == 4) && ($i%6 == 3)
&& !($i%7) && ($i%8 == 1) && !($i%9))
{
array_push($return,$i);
}
}
return $return;
}
//线程数设置为2--可根据服务器来设置一个具体合理的线程值
$urls_array = array(
array('start'=>0,'end'=>5000000),
array('start'=>5000001,'end'=>10000000),
);
$t = microtime ( true );
$result = model_thread_result_get ( $urls_array );
$e = microtime ( true );
echo "多线程:" . ($e - $t) . "<br/>";
echo count($result[0]) + count($result[1])."<br/><br/>";
$t = microtime ( true );
$result_new = array();
foreach ( $urls_array as $key => $value ) {
$result_new = array_merge_recursive($result_new,get_data ($value['start'],$value['end']));
}
$e = microtime ( true );
echo "For循环:" . ($e - $t) . "<br/>";
echo count($result_new)."<br/><br/>";
计算的结果大约是这样子:
多线程:1.7861778736115
3968
For循环:2.5182521343231
3968
大家可以去体会下
一筐鸡蛋:
1个1个拿,正好拿完。
2个2个拿,还剩1个。
3个3个拿,正好拿完。
4个4个拿,还剩1个。
5个5个拿,还剩4个。
6个6个拿,还剩3个。
7个7个拿,正好拿完。
8个8个拿,还剩1个。
9个9个拿,正好拿完。
问筐里有多少鸡蛋?
求高人解答???
在本地计算这个过程,竟然超过了2秒的时间,突然想到多线程这个概念,于是就安装了pthread这个扩展,官网代文档说明:http://php.net/manual/zh/book.pthreads.php,然后跑到张宴的博客http://zyan.cc/pthreads/,根据提示安装成功,
下面就来缩短上面的计算时间(引用大神的代码进行修改):
//thread class
class test_thread_run extends Thread {
public $data;
public $start;
public $end;
public function __construct($start,$end) {
$this->start = $start;
$this->end = $end;
}
public function run() {
$this->data = get_data ($this->start,$this->end);
}
}
function model_thread_result_get($urls_array) {
foreach ( $urls_array as $key => $value ) {
$thread_array [$key] = new test_thread_run ($value['start'],$value['end']);
$thread_array [$key]->start ();
}
foreach ( $thread_array as $thread_array_key => $thread_array_value ) {
while ( $thread_array [$thread_array_key]->isRunning () ) {
usleep ( 10 );
}
if ($thread_array [$thread_array_key]->join ()) {
$variable_data [$thread_array_key] = $thread_array [$thread_array_key]->data;
}
}
return $variable_data;
}
function get_data($start,$end){
$return = array();
for ($i=$start;$i<$end;$i++){
if(!($i%1) && ($i%2 == 1)
&& !($i%3) && ($i%4 == 1)
&& ($i%5 == 4) && ($i%6 == 3)
&& !($i%7) && ($i%8 == 1) && !($i%9))
{
array_push($return,$i);
}
}
return $return;
}
//线程数设置为2--可根据服务器来设置一个具体合理的线程值
$urls_array = array(
array('start'=>0,'end'=>5000000),
array('start'=>5000001,'end'=>10000000),
);
$t = microtime ( true );
$result = model_thread_result_get ( $urls_array );
$e = microtime ( true );
echo "多线程:" . ($e - $t) . "<br/>";
echo count($result[0]) + count($result[1])."<br/><br/>";
$t = microtime ( true );
$result_new = array();
foreach ( $urls_array as $key => $value ) {
$result_new = array_merge_recursive($result_new,get_data ($value['start'],$value['end']));
}
$e = microtime ( true );
echo "For循环:" . ($e - $t) . "<br/>";
echo count($result_new)."<br/><br/>";
计算的结果大约是这样子:
多线程:1.7861778736115
3968
For循环:2.5182521343231
3968
大家可以去体会下
相关文章推荐
- thinkphp 中 使用七牛云上传
- 微博URL短网址生成算法原理及(java版、php版实现实例)
- php代码优化
- WebStorm 11、PhpStorm 10免费激活(不需要注册码)
- thinkphp配置
- zhphpweixinApi 说明文档
- php类库PHP QR Code 二维码
- yii2 后台前后台 前后台登陆、退出问题
- php乱码的解决方法
- php和java的区别(个人感想)
- laravel之发送邮件
- laravel之Qiniu
- PHP的基本语言结构
- PHP获取上个月、下个月、本月的日期(strtotime(),date())
- Matplotlib 教程
- matplotlib核心剖析
- thinkPHP的字母函数
- MyBatis调用存储过程,含有返回结果集、return参数和output参数
- 重置PHPCMS后台登录密码
- Web Api 输出Json格式数据,中文要进行Unicode编码(模拟PHP的json_encode输出)