This article provides a detailed analysis of strtr function efficiency issues. For more information about how to use str_replace or preg_replace, it is said that strtr is very efficient, so the comparison is as follows:
The code is as follows:
$ I = 0;
$ T = microtime (true );
For (; $ I <1000; $ I ++)
{
$ Str = strtr (md5 ($ I), $ p2 );
}
Var_dump (microtime (true)-$ t); // 0.085476875305176
$ T = microtime (true );
For (; $ I <2000; $ I ++)
{
$ Str = preg_replace ($ p, '', md5 ($ I ));
}
Var_dump (microtime (true)-$ t); // 0.09863805770874
The results show that strtr is about 15% more efficient than preg_replace.
Over the weekend, I checked strtr's php source code:
The code is as follows:
PHP_FUNCTION (strtr)
{
Zval ** str, ** from, **;
Int ac = ZEND_NUM_ARGS ();
// Parameter check (the zend_get_parameters_ex function is defined in the zend_api.c file)
If (ac <2 | ac> 3 | zend_get_parameters_ex (ac, & str, & from, & to) = FAILURE ){
WRONG_PARAM_COUNT;
}
// Parameter check
If (ac = 2 & Z_TYPE_PP (from )! = IS_ARRAY ){
Php_error_docref (NULL TSRMLS_CC, E_WARNING, "The second argument is not an array .");
RETURN_FALSE;
}
Convert_to_string_ex (str );
/* Shortcut for empty string */
// Macro Z_STRLEN_PP defined in zend_operators.h
If (Z_STRLEN_PP (str) = 0 ){
RETURN_EMPTY_STRING ();
}
If (ac = 2 ){
Php_strtr_array (return_value, Z_STRVAL_PP (str), Z_STRLEN_PP (str), HASH_OF (* from ));
} Else {
Convert_to_string_ex (from );
Convert_to_string_ex ();
ZVAL_STRINGL (return_value, Z_STRVAL_PP (str), Z_STRLEN_PP (str), 1 );
Php_strtr (Z_STRVAL_P (return_value ),
Z_STRLEN_P (return_value ),
Z_STRVAL_PP (from ),
Z_STRVAL_PP (),
MIN (Z_STRLEN_PP (from ),
Z_STRLEN_PP ()));
}
}
Let's take a look at the php_strtr function:
The code is as follows:
// Trlen is the minimum length of the string str_from and str_to
PHPAPI char * php_strtr (char * str, int len, char * str_from, char * str_to, int trlen)
{
Int I;
Unsigned char xlat [256]; //
If (trlen <1) | (len <1 )){
Return str;
}
// The subscript and value of xlat are equal.
For (I = 0; I <256; xlat [I] = I, I ++ );
// Match each character from string to string. For example: from = "AB", to = "cd", the corresponding 'a' => 'C', 'B' => 'D' will be generated '.
For (I = 0; I <trlen; I ++ ){
Xlat [(unsigned char) str_from [I] = str_to [I];
}
// Replace (however, I think the efficiency of this function can be improved, because if the character to be replaced is only a small part of the entire string, in this way, most assignment operations are meaningless. in this case, it is more efficient to judge and assign values first. Test it if you are free)
For (I = 0; I <len; I ++ ){
Str [I] = xlat [(unsigned char) str [I];
}
Return str;
}
It can be seen that strtr ('abcaaabcd', 'AB', 'efd') operations should be very efficient.
(Note: this operation outputs efcdeeefcd)
Let's look at php_strtr_array:
The code is as follows:
Static void php_strtr_array (zval * return_value, char * str, int slen, HashTable * hash)
{
Zval ** entry;
Char * string_key;
Uint string_key_len;
Zval ** trans;
Zval ctmp;
Ulong num_key;
Int minlen = 128*1024;
Int maxlen = 0, pos, len, found;
Char * key;
HashPosition hpos;
Smart_str result = {0 };
HashTable tmp_hash;
// Copy the replacement array from hash to tmp_hash, and record the maximum and minimum lengths of the subscript string
Zend_hash_init (& tmp_hash, 0, NULL, NULL, 0 );
Zend_hash_internal_pointer_reset_ex (hash, & hpos );
While (zend_hash_get_current_data_ex (hash, (void **) & entry, & hpos) = SUCCESS ){
Switch (zend_hash_get_current_key_ex (hash, & string_key, & string_key_len, & num_key, 0, & hpos )){
Case HASH_KEY_IS_STRING:
Len = string_key_len-1;
If (len <1 ){
Zend_hash_destroy (& tmp_hash );
RETURN_FALSE;
}
Zend_hash_add (& tmp_hash, string_key, string_key_len, entry, sizeof (zval *), NULL );
If (len> maxlen ){
Maxlen = len;
}
If (len <minlen ){
Minlen = len;
}
Break;
// If the subscript is an integer, it will be converted to the string type. for example, convert array (10 => 'A') to array ('10' => 'A ')
Case HASH_KEY_IS_LONG:
Z_TYPE (ctmp) = IS_LONG;
Z_LVAL (ctmp) = num_key;
Convert_to_string (& ctmp );
Len = Z_STRLEN (ctmp );
Zend_hash_add (& tmp_hash, Z_STRVAL (ctmp), len + 1, entry, sizeof (zval *), NULL );
Zval_dtor (& ctmp );
If (len> maxlen ){
Maxlen = len;
}
If (len <minlen ){
Minlen = len;
}
Break;
}
Zend_hash_move_forward_ex (hash, & hpos );
}
Key = emalloc (maxlen + 1 );
Pos = 0;
// Starts loop matching from the first character of the string, and the pos records the current position.
While (pos <slen ){
// Add the maximum length to the current position. if the length is greater than the string length, the maximum length needs to be changed.
If (pos + maxlen)> slen ){
Maxlen = slen-pos;
}
Found = 0;
Memcpy (key, str + pos, maxlen );
// Match from the maximum length, that is, for 'abcd', if array ('a' => 'e', 'AB' => 'F '), instead of replacing a with e.
For (len = maxlen; len> = minlen; len --){
Key [len] = 0;
// This efficiency is quite high because hash tables are used.
If (zend_hash_find (& tmp_hash, key, len + 1, (void **) & trans) = SUCCESS ){
Char * tval;
Int tlen;
Zval tmp;
If (Z_TYPE_PP (trans )! = IS_STRING ){
Tmp = ** trans;
Zval_copy_ctor (& tmp );
Convert_to_string (& tmp );
Tval = Z_STRVAL (tmp );
Tlen = Z_STRLEN (tmp );
} Else {
Tval = Z_STRVAL_PP (trans );
Tlen = Z_STRLEN_PP (trans );
}
// Add result
Smart_str_appendl (& result, tval, tlen );
// Jump forward
Pos + = len;
Found = 1;
If (Z_TYPE_PP (trans )! = IS_STRING ){
Zval_dtor (& tmp );
}
Break;
}
}
If (! Found ){
Smart_str_appendc (& result, str [pos ++]);
}
}
Efree (key );
Zend_hash_destroy (& tmp_hash );
Smart_str_0 (& result );
RETVAL_STRINGL (result. c, result. len, 0 );
}