Php5.2 and 5.3 performance test comparison

Source: Internet
Author: User

Source Address: http://www.cnblogs.com/silvester/archive/2011/04/01/2002114.html

The performance of 5.3 in recursive testing, numeric testing, string operation testing, class and object testing is significantly higher than 5.2, improving by about 15%-40%, but the memory consumption is over 5.3, the IO operation speed is basically the same. Boss Wei Zhi asked me to do the test. The test method may not be absolutely correct. I hope you can give me more advice. The detailed code and test results are as follows:

Test Environment

A. VM + nginx0.7.67 + php5.2.10

B. VM + nginx0.7.67 + php5.3.6

1. Test recursive functions

The exchange sorting algorithm is used to sort the arrays of 10 thousand numeric elements 30 times. This test mainly tests the performance of 5.2 and 5.3 in recursive calling.

<? Phpfunction getmsectime () {$ arr = explode ('', microtime (); return $ arr [0] + $ arr [1];} // start time $ starttime = getmsectime ();/*** fast sort algorithm ** @ Param array $ arr * @ return array */function quicksort ($ ARR) {If (count ($ ARR)> 1) {$ key = $ arr [0]; $ min = array (); $ max = array (); for ($ I = 1; $ I <count ($ ARR); $ I ++) {if ($ arr [$ I]> $ key) {$ MAX [] = $ arr [$ I];} else {$ Min [] = $ arr [$ I] ;}}$ min = Count ($ min)> 0? Quicksort ($ min): $ min; $ max = count ($ max)> 0? Quicksort ($ max): $ Max; return array_merge ($ min, array ($ Key), $ max);} else {return $ arr ;}} // program running time $ runtimenum = 0; $ runtime = 0; For ($ I = 0; $ I <30; $ I ++) {$ arr = range (1, 10000); shuffle ($ ARR); $ arr = quicksort ($ ARR); $ runtimenum ++ ;}$ runtime = (getmsectime ()-$ starttime)/$ runtimenum; echo 'running time: ', $ runtime,' <br> '; echo 'memory usage :'. memory_get_usage ();?>

Test Results

Php5.2.10

Running time: 0.2008661031723

Memory usage: 1540616

Php5.3.6

Running time: 0.17402129968007

Memory usage: 2134104

2. Test the numerical calculation

Perform cyclic addition and subtraction for the specified range value.

<? Phpfunction getmsectime () {$ arr = explode ('', microtime (); return $ arr [0] + $ arr [1];} // start time $ starttime = getmsectime (); $ numarr = array (100,-100,100 0,-1000, 1); // the program running time $ runtime = 0; $ k = 0; $ J = 0; while ($ j <1000000) {$ J ++; $ K + = $ numarr [$ J % 5];} $ runtime = getmsectime ()-$ starttime; echo 'running time: ', $ runtime,' <br> '; echo 'memory usage :'. memory_get_usage ();

Test Results

Php5.2.10

Running time: 0.15911817550659

Memory usage: 81240

Php5.3.6

Running time: 0.13838791847229

Memory usage: 625640

3. String operations

Concatenates a string for 1 million times.

<? Phpfunction getmsectime () {$ arr = explode ('', microtime (); return $ arr [0] + $ arr [1];} // start time $ starttime = getmsectime (); // program running time $ runtime = 0; $ J = 0; $ STR = ''; while ($ j <1000000) {$ J ++; $ Str. = $ J. 'abcdefg';} $ runtime = getmsectime ()-$ starttime; echo 'running time: ', $ runtime,' <br> '; echo 'memory usage :'. memory_get_usage ();

Test Results

Php5.2.10

Running time: 0.30056118965149

Memory usage: 12968168

Php5.3.6

Running time: 0.27767395973206

Memory usage: 13512760


4. Test the class and object speed

Instantiate the specified class 1 million times and call its class method.

<? Phpfunction getmsectime () {$ arr = explode ('', microtime (); return $ arr [0] + $ arr [1];} // start time $ starttime = getmsectime (); Class testclass {public $ Testa; Public $ testb; public function _ construct ($ num) {$ this-> Testa = $ num;} public function getnum () {return $ this-> Testa ;}; // program running time $ runtime = 0; $ J = 0; $ STR = ''; while ($ j <1000000) {$ J ++; $ A = new testclass ($ J ); $ A-> getnum () ;}$ runtime = getmsectime ()-$ starttime; echo 'running time: ', $ runtime,' <br> '; echo 'memory usage: '. memory_get_usage ();

5. file I/O operation speed test

Test creation, deletion, and detection of files and folders, and write data into files.

<? Phpfunction getmsectime () {$ arr = explode ('', microtime (); return $ arr [0] + $ arr [1];} // start time $ starttime = getmsectime (); // program running time $ runtime = 0; $ J = 0; while ($ j <1000) {$ J ++; $ filename = '. /'. $ J. 'test.txt '; If (file_exists ($ filename) {unlink ($ filename);} file_put_contents ($ filename, $ J); $ filedir = '. /'. $ J. 'filedir'; If (is_dir ($ filedir) & file_exists ($ filedir) {rmdir ($ filedir);} mkdir ($ filedir, 0777 ); $ J ++ ;}$ runtime = getmsectime ()-$ starttime; echo 'running time: ', $ runtime,' <br> '; echo 'memory usage :'. memory_get_usage ();

Test Results

Php5.2.10

Running time: 0.056291103363037

Memory usage: 83392

Php5.3.6

Running time: 0.057452917098999

Memory usage: 628000

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.