Last active
January 17, 2017 12:17
-
-
Save hkarasek/3e5a08c3c1f3684a04cb to your computer and use it in GitHub Desktop.
PHP benchmark
This file contains hidden or 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 | |
/* | |
########################################################################## | |
# PHP Benchmark Performance Script mk2 # | |
# # | |
# Author : Jan Karásek # | |
# Company : ---- | |
# Date : August 2, 2014 # | |
# version : 2.0.0 # | |
# License : Creative Commons CC-BY license # | |
# Website : http://hkar.eu # | |
# # | |
# # | |
# forked from: # | |
# Author : Alessandro Torrisi # | |
# Company : Code24 BV, The Netherlands # | |
# Date : July 31, 2010 # | |
# License : Creative Commons CC-BY license # | |
# Website : http://www.php-benchmark-script.com # | |
# # | |
########################################################################## | |
*/ | |
class benchmark | |
{ | |
private static function test_Math($count = 140000) | |
{ | |
$time_start = microtime(true); | |
$mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt"); | |
foreach ($mathFunctions as $key => $function) { | |
if (!function_exists($function)) unset($mathFunctions[$key]); | |
} | |
for ($i = 0; $i < $count; $i++) { | |
foreach ($mathFunctions as $function) { | |
$r = call_user_func_array($function, array($i)); | |
} | |
} | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
private static function test_StringManipulation($count = 130000) | |
{ | |
$time_start = microtime(true); | |
$stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "md5", "sha1", "strtoupper", "strtolower", "strrev", "strlen", "soundex", "ord"); | |
foreach ($stringFunctions as $key => $function) { | |
if (!function_exists($function)) unset($stringFunctions[$key]); | |
} | |
$string = "the quick brown fox jumps over the lazy dog"; | |
for ($i = 0; $i < $count; $i++) { | |
foreach ($stringFunctions as $function) { | |
$r = call_user_func_array($function, array($string)); | |
} | |
} | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
private static function test_Loops($count = 19000000) | |
{ | |
$time_start = microtime(true); | |
for ($i = 0; $i < $count; ++$i) ; | |
$i = 0; | |
while ($i < $count) ++$i; | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
private static function test_IfElse($count = 9000000) | |
{ | |
$time_start = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
if ($i == -1) { | |
} elseif ($i == -2) { | |
} else if ($i == -3) { | |
} | |
} | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
private static function test_arrayManipulation($count = 10000000) | |
{ | |
$data = []; | |
$time_start = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
$data[] = $i; | |
unset($data[$i-1]); | |
} | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
private static function test_fileWriteAndRead($count = 10000) | |
{ | |
$time_start = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
file_put_contents('test.txt', $i); | |
file_get_contents('test.txt'); | |
} | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
private static function test_hashSHA1($count = 1000000) | |
{ | |
$time_start = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
sha1('benchmark'); | |
} | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
private static function test_hashMD5($count = 1000000) | |
{ | |
$time_start = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
md5('benchmark'); | |
} | |
return number_format(microtime(true) - $time_start, 3); | |
} | |
public static function run($echo = true) | |
{ | |
$total = 0; | |
$server = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '?') . '@' . (isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '?'); | |
$methods = get_class_methods('benchmark'); | |
$line = str_pad("-", 38, "-"); | |
$return = "<pre>$line\n|" . str_pad("PHP BENCHMARK SCRIPT", 36, " ", STR_PAD_BOTH) . "|\n$line\nStart : " . date("Y-m-d H:i:s") . "\nServer : $server\nPHP version : " . PHP_VERSION . "\nPlatform : " . PHP_OS . "\n$line\n"; | |
foreach ($methods as $method) { | |
if (preg_match('/^test_/', $method)) { | |
$total += $result = self::$method(); | |
$return .= str_pad($method, 25) . " : " . $result . " sec.\n"; | |
} | |
} | |
$return .= str_pad("-", 38, "-") . "\n" . str_pad("Total time:", 25) . " : " . $total . " sec.</pre>"; | |
if ($echo) echo $return; | |
return $return; | |
} | |
} | |
benchmark::run(); | |
?> |
This file contains hidden or 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 BENCHMARK SCRIPT | | |
-------------------------------------- | |
Start : 2014-08-02 12:13:30 | |
Server : parahos*****[email protected] | |
PHP version : 5.4.21 | |
Platform : Linux | |
-------------------------------------- | |
test_Math : 1.860 sec. | |
test_StringManipulation : 1.903 sec. | |
test_Loops : 1.149 sec. | |
test_IfElse : 0.939 sec. | |
test_arrayManipulation : 1.624 sec. | |
test_fileWriteAndRead : 1.679 sec. | |
test_hashSHA1 : 0.580 sec. | |
test_hashMD5 : 0.500 sec. | |
-------------------------------------- | |
Total time: : 10.234 sec. | |
################################################# | |
-------------------------------------- | |
| PHP BENCHMARK SCRIPT | | |
-------------------------------------- | |
Start : 2014-08-02 12:08:20 | |
Server : 76***[email protected] | |
PHP version : 5.4.28 | |
Platform : Linux | |
-------------------------------------- | |
test_Math : 2.138 sec. | |
test_StringManipulation : 2.315 sec. | |
test_Loops : 1.369 sec. | |
test_IfElse : 1.173 sec. | |
test_arrayManipulation : 2.372 sec. | |
test_fileWriteAndRead : 6.278 sec. | |
test_hashSHA1 : 0.886 sec. | |
test_hashMD5 : 0.624 sec. | |
-------------------------------------- | |
Total time: : 17.155 sec. | |
################################################# | |
-------------------------------------- | |
| PHP BENCHMARK SCRIPT | | |
-------------------------------------- | |
Start : 2014-08-02 10:08:10 | |
Server : ti*****.******.eu@10.***.***.*** | |
PHP version : 5.5.15-1~dotdeb.1 | |
Platform : Linux | |
-------------------------------------- | |
test_Math : 1.057 sec. | |
test_StringManipulation : 1.124 sec. | |
test_Loops : 0.720 sec. | |
test_IfElse : 0.528 sec. | |
test_arrayManipulation : 1.051 sec. | |
test_fileWriteAndRead : 0.195 sec. | |
test_hashSHA1 : 0.420 sec. | |
test_hashMD5 : 0.350 sec. | |
-------------------------------------- | |
Total time: : 5.445 sec. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment