- microtime: documentation ( source)
- strspn: documentation ( source)
<?php
$string1 = 'The lazy cat jumps over the fast fox';
$string2 = 'The fast fox jumps over the lazy cat';
$len=strlen($string1);
const runs=31337;
$start=microtime(1);
$i=0;while (++$i<runs) {
$xor = $string1 ^ $string2;
$pos=0;while(($pos+=strspn($xor,"\0",$pos))<$len) {
// print 'Diff @'.$pos.' String1:'.$string1[$pos].' String2:'.$string2[$pos]." <br>\n";
$pos++;
}
} print "ircmax's XOR+strspn + ka's loop:<br>\n".(microtime(1)-$start)."sec<br>\n<br>\n";
$start=microtime(1);
$i=0;while (++$i<runs) {
for ($offset = 0; $offset < $len; ++$offset) {
if ($string1[$offset] !== $string2[$offset]) {
//print 'Diff @'.$offset.' String1:'.$string1[$offset].' String2:'.$string2[$offset]." <br>\n";
}
}
} print "NikiC's example from question body:<br>\n".(microtime(1)-$start)."sec<br>\n";