Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of the efficiency of strtr function

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of a sample analysis of the efficiency of strtr functions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Recently, we often have to match and replace strings. In the past, we usually used str_replace or preg_replace. It is said that strtr is very efficient, so we compare it with each other:

The code is as follows:

$I = 0

$t = microtime (true)

For (; $iPrecious, 'bounded = >' d'.

For (I = 0; I)

< trlen; i++) { xlat[(unsigned char) str_from[i]] = str_to[i]; } //替换(不过觉得这个函数的效率还有可以改进的地方,因为如果需要替换的字符只是占整个字符串很少的部分,这样就有大部分的赋值操作其实并没有什么意义,这样的情况下感觉先判断再赋值感觉会高效一点。有空测试一下) for (i = 0; i < len; i++) { str[i] = xlat[(unsigned char) str[i]]; } return str; } 可见,在处理strtr('abcdaaabcd', 'ab', 'efd')这样的操作时,应该是很高效的。 (注意:这个操作输出efcdeeefcd) 再看看php_strtr_array: 复制代码 代码如下: 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; //把替换数组从hash复制到tmp_hash,并记录下标字符串的最大和最小长度 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; //下标如果是整形的话会转换成字符串类型,例如:array(10=>

Convert 'aa') to array (' 10 percent = > 'aa')

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; //从字符串的第一个字符开始循环匹配,pos记录当前查找的位置 while (pos < slen) { //当前位置加上最大长度,如果大于字符串长度,则最大长度就需要改变 if ((pos + maxlen) >

Slen) {

Maxlen = slen-pos

}

Found = 0

Memcpy (key, str+pos, maxlen)

/ / match from the maximum length, that is to say, for 'abcd',', if ab is replaced by f instead of a with e first, it will be replaced by an if it is array ('a'= >'e').

For (len = maxlen; len > = minlen; len--) {

Key [len] = 0

/ / because the hash table is used, the efficiency is quite high.

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)

}

/ / join 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)

}

Thank you for reading! This is the end of the article on "example analysis of the efficiency of strtr functions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report