PHP7 RC7 Release对比PHP5.6快速排序20000数据性能体验以及新语法尝鲜_PHP教程_编程技术
最近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官方地址
https://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:
<!--?php function 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++)>
Copy after login1.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的这一举动 也证明了这一点,例如下面代码<!--?php declare(strict_types=1); function GetInt():int{ return 1.0; } echo GetInt(); ?-->
Copy after login
如果把上述代码改为return 1;才能正常运行,否则运行会报错,这就是PHP7的强类型约束,此模式下完全摒弃若类型。<!--?php declare(strict_types=1); function GetInt():int{ return 1; } echo GetInt(); ?-->
Copy after login
2.2 强类型参数
<!--?php declare(strict_types=1); function add(int $a,int $b):int{ return $a+$b; } echo add(1,2); ?-->
Copy after loginvar_dump的结果是 int(3)<!--?php declare(strict_types=1); function add(int $a,int $b):int{ return $a+$b; } var_dump(add(1,2)); ?-->
Copy after login
2.3 返回类型冲突
返回值类型和强类型约束不同 将跑出异常<!--?php declare(strict_types=1); function foobar(float $abc): int { return ceil($abc + 1); } try{ foobar(1.22); }catch(Exception $ex){ echo $ex--->getMessage(); } ?>
Copy after login
OK关于Scalar Type就不一个一个写了
更多的介绍请参考:https://wiki.php.net/rfc/scalar_type_hints_v5#php_rfcscalar_type_declarations
2.4 、关于PHP的 Anonymous Class
简单匿名类继承<!--?php declare(strict_types=1); class Foo {public function M1(){echo "hello,world!";}} $child = new class extends Foo { public function M2(){echo "hello,world!";return $this;}}; $child--->M2()->M1(); ?>
Copy after login
简单的匿名类实例化<!--?php declare(strict_types=1); var_dump(new class(5) { public function __construct($i) { $this--->i = $i; } }); ?>
Copy after login

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...
