note 129441 added to function.str-starts-with

From: Date: Sat, 27 Apr 2024 20:54:00 +0000
Subject: note 129441 added to function.str-starts-with
Groups: php.notes 
Request: Send a blank email to php-notes+get-221307@lists.php.net to get a copy of this message
This function is astonishingly fast, I think you should always use it. See my test code below, uncomment specific lines and run it yourself to see the effect. <?php $str = sha1( '0' ); // b6589fc6ab0dc82cf12099d1c2d40ab994e8410c $results = []; for ( $j = 0; $j < 100; ++ $j ) { $time = microtime( true ); for ( $i = 0; $i < 999999; ++ $i ) // if ( 'b6589f' === substr( $str, 0, 6 ) ); # 0.53781658887863 // if ( 'abcdef' === substr( $str, 0, 6 ) ); # 0.54560388565063 // if ( 0 === strpos( $str, 'b6589f' ) ); # 0.48515980958939 // if ( 0 === strpos( $str, 'abcdef' ) ); # 0.53281677722931 // if ( str_starts_with( $str, 'b6589f' ) ); # 0.29601020097733 // if ( str_starts_with( $str, 'abcdef' ) ); # 0.30969516038895 // if ( // 'b' === $str[0] && '6' === $str[1] && '5' === $str[2] && // '8' === $str[3] && '9' === $str[4] && 'f' === $str[5] // ); # 1.6171971583366 $results[] = microtime( true ) - $time; } echo array_sum( $results ) / 100; ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.131 (proxied: 2408:8207:18a1:cb20:7f9c:e716:d091:8110) ---- Manual Page -- https://php.net/manual/en/function.str-starts-with.php Edit -- https://main.php.net/note/edit/129441 Del: integrated -- https://main.php.net/note/delete/129441/integrated Del: useless -- https://main.php.net/note/delete/129441/useless Del: bad code -- https://main.php.net/note/delete/129441/bad+code Del: spam -- https://main.php.net/note/delete/129441/spam Del: non-english -- https://main.php.net/note/delete/129441/non-english Del: in docs -- https://main.php.net/note/delete/129441/in+docs Del: other reasons-- https://main.php.net/note/delete/129441 Reject -- https://main.php.net/note/reject/129441 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221307) next »