国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP7RC7Release對比PHP5.6快速排序20000數據性能體驗以及新語法嘗鮮

2020-03-22 18:09:14
字體:
來源:轉載
供稿:網友
  • 最近Zend的PHP7已經 處于最后的BUG修復階段,目前 已經更新RC7,對于Zend官方的說法PHP7的性能大約相比PHP5系列版本 提高2倍以上,增加了一些新的語法,摒棄了PHP5的一些影響性能的因素,主要增加了以下Features 。

    Improved performance: PHP 7 is up to twice as fast as PHP 5.6 性能比5.6提高2倍 Consistent 64-bit support 64位一致性支持Many fatal errors are now Exceptions 增加許多致命錯誤異常 Removal of old and unsupported SAPIs and extensions 移除了舊的不支持的 SAPIS 和一些擴展The null coalescing operator (??) 空合并運算符 Combined comparison Operator (<=>) 結合比較運算符 Return Type Declarations 和C語言等一樣 顯示的返回值類型Scalar Type Declarations 標量類型定義Anonymous Classes 匿名類!處于好奇的心態我同時安裝了PHP5.5 以及PHP7 RC7 Release,體驗一下 , 于是分別體驗了 PHP7的 性能提升 以及 新語法,至于怎么安裝配置PHP7相信不用我說了,廢話不多說。
    http://php.net/ php7 RC7 Release官方地址
    http://wiki.php.net/rfc/scalar_type_hints_v5 PHP7 wiki



    1、PHP7與PHP5.5性能對比PHP7的性能相對于PHP5.6提高了多少,下面用一個簡單而且傻逼的代碼來測試一下就知道了,這里我用5.5版本的PHP對比一下吧,電腦上只有5.5了 懶得去安裝PHP5.6。
    如下圖php7和php5.5:

    1.1我這里用一小段傻逼代碼來測試一下PHP:
    <?phpfunction microtime_float(){	list($usec, $sec) = explode(" ", microtime());	return ((float)$usec + (float)$sec);}define('ARRAY_SIZE',20000);function QuickSort($arr,$low,$high){ if($low>$high)   return ; $begin=$low; $end=$high ; $key=$arr[$begin]; while($begin<$end) {	while($begin<$end&&$arr[$end]>=$key)	   --$end ;	$arr[$begin]=$arr[$end];	while($begin<$end&&$arr[$begin]<=$key)	  ++$begin;	$arr[$end]=$arr[$begin];	 }  $arr[$begin]=$key;  QuickSort($arr,$low,$begin-1);  QuickSort($arr,$begin+1,$high);}$time_start = microtime_float();$arr=array();for($i=0;$i<ARRAY_SIZE;$i++){ array_push($arr,rand(1,20000));}QuickSort($arr,0,ARRAY_SIZE-1);$time_end = microtime_float();echo "Bengin:$time_start".'s   ';echo  "End:$time_end".'s  ';echo "TakeTime: ".($time_end-$time_start).'s  ';?>
    1.2 測試結果分別在PHP7和PHP5.5下運行20000隨機數據 快速排序算法之后結果 PHP7是PHP5.5的12倍!!!! 看來PHP7開始要雄起了!

    下面分別是PHP7 RC7和 PHP5運行上述快速排序 20000數據算法的速度。



    2、PHP7新語法體驗2.1、標量類型 和強類型PHP7新增四個標量類型 int, float, string bool, 首先要使用強類型 必須在文件中加入指令
  • declare(strict_types=1)該指令必須是第一個指令而且只有一種用法 所謂嚴格類型強類型的概念就是,我們要摒棄PHP5.6之前的若類型觀念,因為我們知道PHP本身一門若類型語言,正因為如此在類型轉換已經類型檢查導致PHP語言本身性能極為低下php7的這一舉動 也證明了這一點,例如下面代碼
    <?phpdeclare(strict_types=1);function GetInt():int{  return 1.0;}echo GetInt();?>

    如果把上述代碼改為return 1;才能正常運行,否則運行會報錯,這就是PHP7的強類型約束,此模式下完全摒棄若類型。
    <?phpdeclare(strict_types=1);function GetInt():int{  return 1;}echo GetInt();?>

    2.2 強類型參數
    <?phpdeclare(strict_types=1);function add(int $a,int $b):int{	 return $a+$b;}echo add(1,2);?>
    <?phpdeclare(strict_types=1);function add(int $a,int $b):int{	 return $a+$b;}var_dump(add(1,2));?>
    var_dump的結果是 int(3)
    2.3 返回類型沖突返回值類型和強類型約束不同 將跑出異常
    <?phpdeclare(strict_types=1);function foobar(float $abc): int {    return ceil($abc + 1);} try{	foobar(1.22); }catch(Exception $ex){	 echo $ex->getMessage(); }?>

    OK關于Scalar Type就不一個一個寫了
    更多的介紹請參考:http://wiki.php.net/rfc/scalar_type_hints_v5#php_rfcscalar_type_declarations

    2.4 、關于PHP的 Anonymous Class簡單匿名類繼承
    <?phpdeclare(strict_types=1);html' target='_blank'>class Foo {public function M1(){echo 'hello,world!';}}$child = new class extends Foo { public function M2(){echo 'hello,world!';return $this;}};$child->M2()->M1();?>

    簡單的匿名類實例化
    <?phpdeclare(strict_types=1);var_dump(new class(5) {    public function __construct($i) {        $this->i = $i;    }});?>

    更多匿名類的東西參考 http://wiki.php.net/rfc/anonymous_classes


    寫到這里吧,寫多了也沒啥意思了。 相信簡短的介紹足矣說明PHP7 的確很值得讓人期待哦。
    PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 诸城市| 凭祥市| 林口县| 涞源县| 安福县| 万年县| 潍坊市| 洛阳市| 关岭| 闵行区| 自治县| 方正县| 安阳县| 渭南市| 四会市| 广安市| 沽源县| 金阳县| 分宜县| 若羌县| 垦利县| 汕尾市| 阳春市| 龙里县| 和平县| 府谷县| 呼伦贝尔市| 武乡县| 清流县| 洛阳市| 庆安县| 上杭县| 大石桥市| 浑源县| 平遥县| 荥经县| 青铜峡市| 海林市| 黑龙江省| 六安市| 临沂市|