Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created November 18, 2011 00:20
Show Gist options
  • Save mikeschinkel/1375073 to your computer and use it in GitHub Desktop.
Save mikeschinkel/1375073 to your computer and use it in GitHub Desktop.
Testing suppression of error using @sign
<?php
/*
TESTING:
http://programmers.stackexchange.com/questions/120378/is-error-suppression-acceptable-in-role-of-logic-mechanism/120386#120386
Elapsed Time[µseconds]
Check, Exists :
50000 - Check, Exists : 94718 (user), 342 (system)
EACH - Check, Exists : 1.89436E-6 (user), 6.84E-9 (system)
Suppress, Exists :
50000 - Suppress, Exists : 146815 (user), 170 (system)
EACH - Suppress, Exists : 2.9363E-6 (user), 3.4E-9 (system)
Check, Missing :
50000 - Check, Missing : 84252 (user), 82 (system)
EACH - Check, Missing : 1.68504E-6 (user), 1.64E-9 (system)
Suppress, Missing:
50000 - Suppress, Missing: 200548 (user), 297 (system)
EACH - Suppress, Missing: 4.01096E-6 (user), 5.94E-9 (system)
Exists : 1.6 times slower (user), 0.5 (system) times slower.
Missing: 2.4 times slower (user), 3.6 (system) times slower.
*/
define( 'TIMES', 50000 );
$results = array();
$messages = array(
'Check, Exists ',
'Suppress, Exists ',
'Check, Missing ',
'Suppress, Missing',
);
$results[] = run_test( 'test_error_check', array('key'=>'foo','foo'=>'bar' ) );
$results[] = run_test( 'test_error_suppress', array('key'=>'foo','foo'=>'bar' ) );
$results[] = run_test( 'test_error_check', array('key'=>'foo' ) );
$results[] = run_test( 'test_error_suppress', array('key'=>'foo' ) );
$times = TIMES;
$denominator = pow(10,6)*TIMES;
echo "Elapsed Time[µseconds]";
foreach( $results as $index => $result ) {
echo "\n{$messages[$index]}:";
echo "\n\t{$times} - {$messages[$index]}: {$result[0]} (user), {$result[1]} (system)";
$result[0] /= $denominator;
$result[1] /= $denominator;
echo "\n\tEACH - {$messages[$index]}: {$result[0]} (user), {$result[1]} (system)";
}
echo "\n";
$messages = array(
'Exists ',
'Missing',
);
for($i=0; $i<2; $i++) {
$j = $i*2;
$uratio = round($results[$j+1][0]/$results[$j][0],1);
$sratio = round($results[$j+1][1]/$results[$j][1],1);
echo "\n{$messages[$i]}: {$uratio} times slower (user), {$sratio} (system) times slower.";
}
echo "\n\n";
function test_error_suppress( $args, $key ) {
return @$args[$key];
}
function test_error_check( $args, $key ) {
return ! empty( $args[$key] ) ? $args[$key] : false;
}
function run_test( $func_to_test, $args = array() ) {
$dat = getrusage();
$utime_before = $dat["ru_utime.tv_sec"].$dat["ru_utime.tv_usec"];
$stime_before = $dat["ru_stime.tv_sec"].$dat["ru_stime.tv_usec"];
for( $i = 0; $i < TIMES; $i++ )
call_user_func( $func_to_test, $args, $args['key'] );
$dat = getrusage();
$utime_after = $dat["ru_utime.tv_sec"].$dat["ru_utime.tv_usec"];
$stime_after = $dat["ru_stime.tv_sec"].$dat["ru_stime.tv_usec"];
$utime_elapsed = ($utime_after - $utime_before);
$stime_elapsed = ($stime_after - $stime_before);
return array( $utime_elapsed, $stime_elapsed );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment