Skip to content

Instantly share code, notes, and snippets.

@rybakit
Created July 14, 2016 08:14
Show Gist options
  • Save rybakit/7288dc9ba86e22c260684adde988e2c1 to your computer and use it in GitHub Desktop.
Save rybakit/7288dc9ba86e22c260684adde988e2c1 to your computer and use it in GitHub Desktop.
switch gettype vs if is_*
<?php
function switch_gettype($n, $value) {
for ($i = 0; $i < $n; ++$i) {
switch (\gettype($value)) {
case 'array': continue;
case 'string': continue;
case 'integer': continue;
case 'NULL': continue;
case 'boolean': continue;
case 'double': continue;
}
}
}
function if_is_func($n, $value) {
for ($i = 0; $i < $n; ++$i) {
if (\is_array($value)) {
continue;
}
if (\is_string($value)) {
continue;
}
if (\is_int($value)) {
continue;
}
if (null === $value) {
continue;
}
if (\is_bool($value)) {
continue;
}
if (\is_double($value)) {
continue;
}
}
}
function switch_is_func($n, $value) {
for ($i = 0; $i < $n; ++$i) {
switch (true) {
case \is_array($value): continue;
case \is_string($value): continue;
case \is_int($value): continue;
case (null === $value): continue;
case \is_bool($value): continue;
case \is_double($value): continue;
}
}
}
function empty_loop($n) {
for ($i = 0; $i < $n; ++$i) {
}
}
function getmicrotime()
{
$t = gettimeofday();
return ($t['sec'] + $t['usec'] / 1000000);
}
function start_test()
{
ob_start();
return getmicrotime();
}
function end_test($start, $name, $overhead = null)
{
global $total;
global $last_time;
$end = getmicrotime();
ob_end_clean();
$last_time = $end-$start;
$total += $last_time;
$num = number_format($last_time,3);
$pad = str_repeat(" ", 30 - strlen($name) - strlen($num));
if (is_null($overhead)) {
echo $name.$pad.$num."\n";
} else {
$num2 = number_format($last_time - $overhead, 3);
echo $name.$pad.$num." ".$num2."\n";
}
ob_start();
return getmicrotime();
}
function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad."\n";
$num = number_format($total,3);
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
echo "Total".$pad.$num."\n";
}
const N = 50000000;
const VALUE_STR = "str";
const VALUE_INT = 0;
const VALUE_FLOAT = 0.0;
const VALUE_NULL = null;
const VALUE_ARR = [];
const VALUE_BOOL = true;
$obj = (object)[];
$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
switch_gettype(N, VALUE_STR);
$t = end_test($t, 'switch_gettype(str)', $overhead);
if_is_func(N, VALUE_STR);
$t = end_test($t, 'if_is_func(str)', $overhead);
switch_is_func(N, VALUE_STR);
$t = end_test($t, 'switch_is_func(str)', $overhead);
switch_gettype(N, VALUE_INT);
$t = end_test($t, 'switch_gettype(int)', $overhead);
if_is_func(N, VALUE_INT);
$t = end_test($t, 'if_is_func(int)', $overhead);
switch_is_func(N, VALUE_INT);
$t = end_test($t, 'switch_is_func(int)', $overhead);
switch_gettype(N, VALUE_FLOAT);
$t = end_test($t, 'switch_gettype(float)', $overhead);
if_is_func(N, VALUE_FLOAT);
$t = end_test($t, 'if_is_func(float)', $overhead);
switch_is_func(N, VALUE_FLOAT);
$t = end_test($t, 'switch_is_func(float)', $overhead);
switch_gettype(N, VALUE_NULL);
$t = end_test($t, 'switch_gettype(null)', $overhead);
if_is_func(N, VALUE_NULL);
$t = end_test($t, 'if_is_func(null)', $overhead);
switch_is_func(N, VALUE_NULL);
$t = end_test($t, 'switch_is_func(null)', $overhead);
switch_gettype(N, VALUE_ARR);
$t = end_test($t, 'switch_gettype(arr)', $overhead);
if_is_func(N, VALUE_ARR);
$t = end_test($t, 'if_is_func(arr)', $overhead);
switch_is_func(N, VALUE_ARR);
$t = end_test($t, 'switch_is_func(arr)', $overhead);
switch_gettype(N, VALUE_BOOL);
$t = end_test($t, 'switch_gettype(bool)', $overhead);
if_is_func(N, VALUE_BOOL);
$t = end_test($t, 'if_is_func(bool)', $overhead);
switch_is_func(N, VALUE_BOOL);
$t = end_test($t, 'switch_is_func(bool)', $overhead);
switch_gettype(N, $obj);
$t = end_test($t, 'switch_gettype(obj)', $overhead);
if_is_func(N, $obj);
$t = end_test($t, 'if_is_func(obj)', $overhead);
switch_is_func(N, $obj);
$t = end_test($t, 'switch_is_func(obj)', $overhead);
total();
$ php tests/bench_gettype_vs_is.php
empty_loop 0.399
switch_gettype(str) 3.641 3.241
if_is_func(str) 0.788 0.389
switch_is_func(str) 0.787 0.388
switch_gettype(int) 3.933 3.533
if_is_func(int) 0.993 0.593
switch_is_func(int) 1.019 0.620
switch_gettype(float) 5.128 4.728
if_is_func(float) 1.719 1.320
switch_is_func(float) 1.798 1.399
switch_gettype(null) 4.117 3.717
if_is_func(null) 1.229 0.829
switch_is_func(null) 1.224 0.825
switch_gettype(arr) 3.257 2.858
if_is_func(arr) 0.590 0.190
switch_is_func(arr) 0.586 0.187
switch_gettype(bool) 4.604 4.205
if_is_func(bool) 1.449 1.049
switch_is_func(bool) 1.473 1.074
switch_gettype(obj) 4.933 4.533
if_is_func(obj) 1.698 1.298
switch_is_func(obj) 1.834 1.435
------------------------
Total 47.197
$ php -v
PHP 7.0.5 (cli) (built: Apr 23 2016 10:48:01) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment