


Detailed explanation of simple performance test code examples for php7
Test version:
Old version PHP 5.5.29, new version PHP 7.0.0
Test method:
class PerformanceTest { private $time; private $memory; public function begin() { $this->time = $this->getTime(); $this->memory = $this->getMemory(); } public function end() { $this->time = $this->getTime() - $this->time; $this->time = round($this->time,7);//在这里才能格式化时间 $this->memory = $this->getMemory() - $this->memory; $this->memory = $this->convert($this->memory); echo "time:{$this->time}秒<br />"; echo "memory:{$this->memory}<br />"; } public function getTime() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } public function getMemory() { return memory_get_usage(); } public function convert($size) { $unit=array('b','kb','mb','gb','tb','pb'); return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; } }
1. Generate half a million arrays , and query 500,000 times whether key exists
$a = array(); $b = new PerformanceTest(); $b->begin(); for($i=0;$i<500000;$i++){ $a[$i] = $i; } foreach($a as $i) { array_key_exists($i, $a); } $b->end(); /*** php 5.5n time:0.131秒 memory:40.15 mb php 7.0n time:0.0780001秒 memory:14 mb ***/
The test results are as follows:
➜ time php test.php
php test.php
0.60 s user
0.05s system
98% cpu
0.667 total
➜ time /usr/local/php7/bin/php test.php
/usr/local/php7/bin/php test.php
0.05s user
0.02s system
92% cpu
0.073 total
PHP7 is 9 times faster than PHP5.5
2. Generate 500,000 arrays and query 500,000 times whether value exists
$a = array(); $b = new PerformanceTest(); $b->begin(); for($i=0;$i<10000;$i++){ $a[$i] = $i; } foreach($a as $i) { in_array($i, $a); } $b->end(); /*** php 5.5n time:0.6560001秒 memory:845.49 kb php 7.0n time:0.095秒 memory:388 kb ***/
➜ time php test.php
php test.php
0.79s user
0.01s system
99% cpu
0.809 total
➜ time /usr/local/php7/bin/php test.php
/usr/local/php7/bin/php test.php
0.08s user
0.01s system
97% cpu
0.091 total
PHP7 is 8.7 times faster than PHP5.5
The above is the detailed content of Detailed explanation of simple performance test code examples for php7. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

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

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
