Created
February 8, 2012 20:27
-
-
Save infynyxx/1773160 to your computer and use it in GitHub Desktop.
PHP speed – empty VS isset VS === with error suppression
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
<? | |
$myvar = array(); | |
$times = 1000000; | |
$start = microtime(true); | |
for($x=0;$x<$times;$x++){ | |
if( @$myvar['test'] === null ) { } | |
} | |
$end = microtime(true); | |
$duration = $end-$start; | |
printf("Test null: %s \n", $duration); | |
$start = microtime(true); | |
for($x=0;$x<$times;$x++){ | |
if( !isset( $myvar['test'] )) { } | |
} | |
$end = microtime(true); | |
$duration = $end-$start; | |
printf("Test isset: %s \n", $duration); | |
$start = microtime(true); | |
for($x=0;$x<$times;$x++){ | |
if( empty( $myvar['test'] )) { } | |
} | |
$end = microtime(true); | |
$duration = $end-$start; | |
printf("Test empty: %s \n", $duration); | |
// populate | |
$myvar['test'] = true; | |
$start = microtime(true); | |
for($x=0;$x<$times;$x++){ | |
if( @$myvar['test'] === null ) { } | |
} | |
$end = microtime(true); | |
$duration = $end-$start; | |
printf("Test null: %s \n", $duration); | |
$start = microtime(true); | |
for($x=0;$x<$times;$x++){ | |
if( !isset( $myvar['test'] )) { } | |
} | |
$end = microtime(true); | |
$duration = $end-$start; | |
printf("Test isset: %s \n", $duration); | |
$start = microtime(true); | |
for($x=0;$x<$times;$x++){ | |
if( empty( $myvar['test'] )) { } | |
} | |
$end = microtime(true); | |
$duration = $end-$start; | |
printf("Test empty: %s \n", $duration); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A suggestion,
<? ?>
Avoid php short open tags.. because most won't enable short open tags.and
Its better to have php full tag
<?php ?>
, It will work whethershort_open_tag
is enabled/disabled, its better to avoid php short tags.❤ Just an suggestion (Got the code through search) Hope this will help others too..
Thank you.