Skip to content

Instantly share code, notes, and snippets.

@jmwebservices
Last active December 4, 2019 17:55
Show Gist options
  • Save jmwebservices/3cd9cb273ec52f835a6dd13d62b2bc76 to your computer and use it in GitHub Desktop.
Save jmwebservices/3cd9cb273ec52f835a6dd13d62b2bc76 to your computer and use it in GitHub Desktop.
Show that strpos() on a chunk using substr() is more efficient than on the whole for large strings.
function find( ?string $string )
{
static $found = 0;
if( null === $string ) return $found;
$found += 0 === strpos( $string, BOM_UTF8 );
$found += 0 === strpos( $string, BOM_UTF16_BE );
$found += 0 === strpos( $string, BOM_UTF16_LE );
$found += 0 === strpos( $string, BOM_UTF32_BE );
$found += 0 === strpos( $string, BOM_UTF32_LE );
}
function random_string( int $length = 32, string $pool = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', string $encoding = null ) : string
{
if( $length === 0 )
return '';
if( $length < 0 )
throw new DomainException( 'Length must be >= 0.' );
if( $pool === '' )
throw new DomainException( 'Pool cannot be an empty string.' );
$encoding = (array) $encoding;
$maxKey = mb_strlen( $pool, ...$encoding );
if( $maxKey === false )
throw new DomainException( 'Invalid encoding specified.' );
for( --$maxKey, $random = ''; $length-- ; )
$random .= mb_substr( $pool, random_int( 0, $maxKey ), 1, ...$encoding );
return $random;
}
const BOM_UTF8 = "\xEF\xBB\xBF";
const BOM_UTF16_BE = "\xFE\xFF";
const BOM_UTF16_LE = "\xFF\xFE";
const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
const BOM_UTF32_LE = "\xFF\xFE\x00\x00";
$bomLess = random_string( 1024 * 1024 );
$tests = 1;
$full = -hrtime( true );
for( $i = 0; $i < $tests; ++$i )
find( $bomLess );
$full += hrtime( true );
$chunk = -hrtime( true );
for( $i = 0; $i < $tests; ++$i )
find( substr( $bomLess, 0, 4 ) );
$chunk += hrtime( true );
var_dump(
'Found: ' . find( null ),
"Finding on full took: $full",
"Finding on chunk took: $chunk"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment