Created
November 27, 2013 12:35
-
-
Save tivnet/7674956 to your computer and use it in GitHub Desktop.
OOP vs. Procedural PHP Benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* OOP vs. Procedural PHP Benchmark | |
* @author tivnet | |
* @version 13.11.27 | |
* Disclaimer: This is too simple, and does not prove anything. Just forget it. :-) | |
* My results on PHP 5.4 / Windows 8.1 / Apache 2.4 (all 64 bits, but who cares) : | |
* first 3 tests: same 3.1 to 3.4; the last test: 5.0 to 5.1 | |
*/ | |
header( 'Content-type: text/plain' ); | |
define( 'CRLF', "\n" ); | |
define( 'REPEAT', 10000000 ); | |
/** | |
* Procedural | |
* @param string $a | |
* @param string $b | |
* @return string | |
*/ | |
function ab( $a = '', $b = '' ) { | |
return $a . $b; | |
} | |
/** | |
* Class MyAB | |
*/ | |
class MyAB { | |
/** | |
* OOP - static | |
* @param string $a | |
* @param string $b | |
* @return string | |
*/ | |
public static function ab( $a = '', $b = '' ) { | |
return $a . $b; | |
} | |
/** | |
* OOP - dynamic | |
* @param string $a | |
* @param string $b | |
* @return string | |
*/ | |
public function abD( $a = '', $b = '' ) { | |
return $a . $b; | |
} | |
} | |
$ab = ''; | |
/** | |
* Procedural | |
*/ | |
$time = microtime( true ); | |
for ( $i = 1; $i < REPEAT; $i ++ ) { | |
$ab = ab(); | |
} | |
echo microtime( true ) - $time; | |
echo CRLF; | |
/** | |
* OOP - static | |
*/ | |
$time = microtime( true ); | |
for ( $i = 1; $i < REPEAT; $i ++ ) { | |
$ab = MyAB::ab(); | |
} | |
echo microtime( true ) - $time; | |
echo CRLF; | |
/** | |
* OOP - dynamic | |
*/ | |
$oAB = new MyAB; | |
$time = microtime( true ); | |
for ( $i = 1; $i < REPEAT; $i ++ ) { | |
$ab = $oAB->abD(); | |
} | |
echo microtime( true ) - $time; | |
echo CRLF; | |
/** | |
* OOP - new object in loop | |
*/ | |
$time = microtime( true ); | |
for ( $i = 1; $i < REPEAT; $i ++ ) { | |
$oAB1 = new MyAB; | |
$ab = $oAB1->abD(); | |
} | |
echo microtime( true ) - $time; | |
echo CRLF; | |
# --- EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment