Last active
August 29, 2015 14:10
-
-
Save zonuexe/e8df881a692f45caf488 to your computer and use it in GitHub Desktop.
PHP Array benchmark generator
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 | |
class KlassStorage | |
{ | |
public static $array = null; | |
public static function get() | |
{ | |
if (!self::$array) { | |
self::$array = [ | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
]; | |
} | |
return self::$array; | |
} | |
} | |
class StaticStorage | |
{ | |
public static function get() | |
{ | |
static $array = [ | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
]; | |
return $array; | |
} | |
} | |
class DirectStorage | |
{ | |
public static function get() | |
{ | |
return [ | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
]; | |
} | |
} | |
function func_static() | |
{ | |
static $array = [ | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
]; | |
return $array; | |
} | |
function func_direct() | |
{ | |
return [ | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
"aaaaaaaaaa", | |
]; | |
} | |
function run_php($function) | |
{ | |
$call_func = $function . '();'. $function . '();'. $function . '();'; | |
$code = 'php -r"include(\"' . $_SERVER['SCRIPT_NAME'] . '\"); ' . $call_func .'"'; | |
//echo PHP_EOL . $code . PHP_EOL; | |
return system($code); | |
} | |
if (realpath($_SERVER['SCRIPT_NAME']) === __FILE__) { | |
$times = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 1; | |
echo "Benchmark ($times times run)" . PHP_EOL; | |
echo "10 Bytes x 10 elements" . PHP_EOL; | |
echo "PHP " . phpversion() . PHP_EOL; | |
$static_result = ['func' => [], 'class' => [], 'method' => []]; | |
$direct_result = ['func' => [], 'method' => []]; | |
$static_other_result = ['func' => [], 'class' => [], 'method' => []]; | |
$direct_other_result = ['func' => [], 'method' => []]; | |
$try = 5; | |
for ($t = 0; $t < $try; $t++) { | |
printf(PHP_EOL . "[%d] ", $t + 1); | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
func_static(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$static_result['func'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
func_direct(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$direct_result['func'][] = $time; | |
echo ($static_result['func'][$t] < $direct_result['func'][$t]) ? 'static' : 'direct'; | |
echo PHP_EOL . "class "; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
KlassStorage::get(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$static_result['class'][] = $time; | |
echo PHP_EOL . "method "; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
StaticStorage::get(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$static_result['method'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
DirectStorage::get(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$direct_result['method'][] = $time; | |
echo ($static_result['method'][$t] < $direct_result['method'][$t]) ? 'static' : 'direct'; | |
echo PHP_EOL . " "; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('func_static'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$static_other_result['func'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('func_direct'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$direct_other_result['func'][] = $time; | |
echo ($static_other_result['func'][$t] < $direct_other_result['func'][$t]) ? 'static' : 'direct'; | |
echo PHP_EOL . "class "; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('KlassStorage::get'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$static_other_result['class'][] = $time; | |
echo PHP_EOL . "method "; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('StaticStorage::get'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$static_other_result['method'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('DirectStorage::get'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%-10f ", $time); | |
$direct_other_result['method'][] = $time; | |
echo ($static_other_result['method'][$t] < $direct_other_result['method'][$t]) ? 'static' : 'direct'; | |
} | |
echo PHP_EOL . PHP_EOL; | |
echo "average : " . PHP_EOL; | |
printf(" static_func: %f sec.". PHP_EOL, array_sum($static_result['func']) / $try); | |
printf(" direct_func: %f sec.". PHP_EOL, array_sum($direct_result['func']) / $try); | |
printf(" static_class: %f sec.". PHP_EOL, array_sum($static_result['class']) / $try); | |
printf(" static_method: %f sec.". PHP_EOL, array_sum($static_result['method']) / $try); | |
printf(" direct_method: %f sec.". PHP_EOL, array_sum($direct_result['method']) / $try); | |
printf(" static_other_func: %f sec.". PHP_EOL, array_sum($static_other_result['func']) / $try); | |
printf(" direct_other_func: %f sec.". PHP_EOL, array_sum($direct_other_result['func']) / $try); | |
printf(" static_other_class: %f sec.". PHP_EOL, array_sum($static_other_result['class']) / $try); | |
printf(" static_other_method: %f sec.". PHP_EOL, array_sum($static_other_result['method']) / $try); | |
printf(" direct_other_method: %f sec.". PHP_EOL, array_sum($direct_other_result['method']) / $try); | |
} |
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
#!/usr/bin/env ruby | |
# PHP constant value Benchmark generator | |
# | |
# @author USAMI Kenta <tadsan@zonu.me> | |
# @license http://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html | |
# Copying and distribution of this file, with or without modification, | |
# are permitted in any medium without royalty provided the copyright | |
# notice and this notice are preserved. This file is offered as-is, | |
# without any warranty. | |
bytes = ARGV[0].to_i | |
elems = ARGV[1].to_i | |
Dir.mkdir 'src' unless Dir.exists? 'src' | |
file = open("src/#{bytes}x#{elems}.php", ?w) | |
file.puts template[:header] | |
body = ("\"#{(?a * bytes)}\",\n") * elems | |
%w(klass_static method_static method_direct static direct).each do |type| | |
file.puts template[type.intern][0] | |
file.puts body | |
file.puts template[type.intern][1] | |
end | |
file.puts template[:footer] % [bytes, elems] | |
BEGIN { | |
def template | |
template = {klass_static: [], method_static: [], method_direct: [], static: [], direct: []} | |
template[:header] = <<EOS | |
<?php | |
EOS | |
template[:klass_static] << <<EOS | |
class KlassStorage | |
{ | |
public static $array = null; | |
public static function get() | |
{ | |
if (!self::$array) { | |
self::$array = [ | |
EOS | |
template[:klass_static] << <<EOS | |
]; | |
} | |
return self::$array; | |
} | |
} | |
EOS | |
template[:method_static] << <<EOS | |
class StaticStorage | |
{ | |
public static function get() | |
{ | |
static $array = [ | |
EOS | |
template[:method_static] << <<EOS | |
]; | |
return $array; | |
} | |
} | |
EOS | |
template[:method_direct] << <<EOS | |
class DirectStorage | |
{ | |
public static function get() | |
{ | |
return [ | |
EOS | |
template[:method_direct] << <<EOS | |
]; | |
} | |
} | |
EOS | |
template[:static] << <<EOS | |
function func_static() | |
{ | |
static $array = [ | |
EOS | |
template[:static] << <<EOS | |
]; | |
return $array; | |
} | |
EOS | |
template[:direct] << <<EOS | |
function func_direct() | |
{ | |
return [ | |
EOS | |
template[:direct] << <<EOS | |
]; | |
} | |
EOS | |
template[:footer] = <<EOS | |
function run_php($function) | |
{ | |
$call_func = $function . '();'. $function . '();'. $function . '();'; | |
$code = 'php -r"include(\\"' . $_SERVER['SCRIPT_NAME'] . '\\"); ' . $call_func .'"'; | |
//echo PHP_EOL . $code . PHP_EOL; | |
return system($code); | |
} | |
if (realpath($_SERVER['SCRIPT_NAME']) === __FILE__) { | |
$times = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 1; | |
echo "Benchmark ($times times run)" . PHP_EOL; | |
echo "%i Bytes x %i elements" . PHP_EOL; | |
echo "PHP " . phpversion() . PHP_EOL; | |
$static_result = ['func' => [], 'class' => [], 'method' => []]; | |
$direct_result = ['func' => [], 'method' => []]; | |
$static_other_result = ['func' => [], 'class' => [], 'method' => []]; | |
$direct_other_result = ['func' => [], 'method' => []]; | |
$try = 5; | |
for ($t = 0; $t < $try; $t++) { | |
printf(PHP_EOL . "[%%d]\t", $t + 1); | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
func_static(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$static_result['func'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
func_direct(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$direct_result['func'][] = $time; | |
echo ($static_result['func'][$t] < $direct_result['func'][$t]) ? 'static' : 'direct'; | |
echo PHP_EOL . "class\t"; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
KlassStorage::get(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$static_result['class'][] = $time; | |
echo PHP_EOL . "method\t"; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
StaticStorage::get(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$static_result['method'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { for ($j = 0; $j < 100; $j++) | |
DirectStorage::get(); | |
} | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$direct_result['method'][] = $time; | |
echo ($static_result['method'][$t] < $direct_result['method'][$t]) ? 'static' : 'direct'; | |
echo PHP_EOL . "\t"; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('func_static'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$static_other_result['func'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('func_direct'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$direct_other_result['func'][] = $time; | |
echo ($static_other_result['func'][$t] < $direct_other_result['func'][$t]) ? 'static' : 'direct'; | |
echo PHP_EOL . "class\t"; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('KlassStorage::get'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$static_other_result['class'][] = $time; | |
echo PHP_EOL . "method\t"; | |
echo "static/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('StaticStorage::get'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$static_other_result['method'][] = $time; | |
echo "direct/"; | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { run_php('DirectStorage::get'); } | |
$end = microtime(true); | |
$time = $end - $begin; | |
printf("%%-10f\t", $time); | |
$direct_other_result['method'][] = $time; | |
echo ($static_other_result['method'][$t] < $direct_other_result['method'][$t]) ? 'static' : 'direct'; | |
} | |
echo PHP_EOL . PHP_EOL; | |
echo "average : " . PHP_EOL; | |
printf("\tstatic_func:\t\t%%f sec.". PHP_EOL, array_sum($static_result['func']) / $try); | |
printf("\tdirect_func:\t\t%%f sec.". PHP_EOL, array_sum($direct_result['func']) / $try); | |
printf("\tstatic_class:\t\t%%f sec.". PHP_EOL, array_sum($static_result['class']) / $try); | |
printf("\tstatic_method:\t\t%%f sec.". PHP_EOL, array_sum($static_result['method']) / $try); | |
printf("\tdirect_method:\t\t%%f sec.". PHP_EOL, array_sum($direct_result['method']) / $try); | |
printf("\tstatic_other_func:\t%%f sec.". PHP_EOL, array_sum($static_other_result['func']) / $try); | |
printf("\tdirect_other_func:\t%%f sec.". PHP_EOL, array_sum($direct_other_result['func']) / $try); | |
printf("\tstatic_other_class:\t%%f sec.". PHP_EOL, array_sum($static_other_result['class']) / $try); | |
printf("\tstatic_other_method:\t%%f sec.". PHP_EOL, array_sum($static_other_result['method']) / $try); | |
printf("\tdirect_other_method:\t%%f sec.". PHP_EOL, array_sum($direct_other_result['method']) / $try); | |
} | |
EOS | |
template | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment