php - Fastest way to get the next element in a string from a needle -
i'll start off code:
$characterset = 'abcdefg'; $character = 'a';
say have 2 variables, characterset
, character
, , want next character
, did:
$characterset = 'abcdefg'; $character = 'a'; $ncharacter = $characterset[ strpos($characterset, $character) + 1 ];
and worked, i'm using method has thousands of times per / second
, , using xdebug + webgrind
result many, many calls strpos
, making function go lot slower should going, realise doing math
in language faster doing function calls math, , have tried using:
$character = 'a'; $ncharacter = chr( ord($character) + 1 );
and work, gives me many characters , if wanted make sure didn't +1
character :
or etc, it'd bunch of conditional arguments, making slower, looking cheap way character -> nextcharacter (determined characterset)
doesn't use lot of resources, benchmarks webgrind
preferred.
thanks :-)
use this:
# character set we're using iterate. $characterset = 'abcdefg'; $chr_array = str_split($characterset); foreach($chr_array $character){ //do whatever character, everytime provide next 1 when foreach loop continues }
i hope helps
Comments
Post a Comment