PHP_FUNCTION(magic_replace)
{
char* text;
int text_length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &text_length) == FAILURE) {
return;
}
memset(text + 1, '*', text_length -2);
RETURN_STRING(text, 1);
}
function f($s){return str_pad($s[0],$l=strlen($s)-1,'*').$s[$l];}
$loops = 100000;
$string = str_pad('', 10000, '0');
$timer = new Timer();
$timer->start();
for($i = 0; $i < $loops; $i++){
f($string);
}
$timer->stop();
echo 'f()' . $timer . "\n";
$timer->start();
for($i = 0; $i < $loops; $i++){
magic_replace($string);
}
$timer->stop();
echo 'magic_replace() ' . $timer . "\n";
f()15.173057079315
magic_replace() 0.45169615745544
:P